Obs*_*ver 4 regression r time-series
以下是我的数据
> x
day sum
1 2015-04-14 129
2 2015-04-15 129
3 2015-04-16 129
4 2015-04-17 899
5 2015-04-18 899
6 2015-04-19 899
7 2015-04-20 899
8 2015-04-21 899
9 2015-04-22 899
10 2015-04-23 899
11 2015-04-24 899
12 2015-04-25 899
13 2015-04-26 899
14 2015-04-27 899
15 2015-04-28 899
16 2015-04-29 899
17 2015-04-30 899
18 2015-05-01 899
19 2015-05-02 899
20 2015-05-03 899
21 2015-05-04 899
22 2015-05-05 899
23 2015-05-06 899
24 2015-05-07 899
25 2015-05-08 899
26 2015-05-09 899
27 2015-05-10 899
28 2015-05-11 899
29 2015-05-12 920
30 2015-05-13 920
31 2015-05-14 920
32 2015-05-15 920
33 2015-05-16 920
34 2015-05-17 920
35 2015-05-18 920
36 2015-05-19 920
37 2015-05-20 920
38 2015-05-21 920
39 2015-05-22 920
40 2015-05-23 920
41 2015-05-24 920
42 2015-05-25 920
43 2015-05-26 920
44 2015-05-27 920
45 2015-05-28 920
46 2015-05-29 920
47 2015-05-30 920
48 2015-05-31 920
49 2015-06-01 1076
Run Code Online (Sandbox Code Playgroud)
我想做一个回归分析来找出总和变为 6000 时的数据。我做了以下操作,
> q<-lm(day ~ sum, data=x)
> q
Call:
lm(formula = day ~ sum, data = x)
Coefficients:
(Intercept) sum
1.653e+04 3.584e-02
> as.Date(predict(q, data.frame(sum=6000)))
1
"2015-11-08"
Run Code Online (Sandbox Code Playgroud)
我能够预测日期。但我不确定准确性,希望提高它。但我无法查看回归的摘要。我收到以下错误,
> summary(q)
Error in Ops.difftime((f - mean(f)), 2) :
'^' not defined for "difftime" objects
Run Code Online (Sandbox Code Playgroud)
以防万一,变量类型很重要,
> typeof(x)
[1] "list"
> typeof(x$day)
[1] "double"
> typeof(x$sum)
[1] "integer"
> class(x$day)
[1] "Date"
Run Code Online (Sandbox Code Playgroud)
之前浏览过一个论坛的时候,
给出以下作为解决方案,
我通过将索引从时间值更改为标准整数索引来解决这个问题,一切都运行良好。
但我不知道该怎么做?
有人可以帮我解决这个问题并告诉我我需要做什么才能得到这里的摘要吗?
谢谢
中的日期R实际上只是按照一定规则重铸为日期格式的数值。例如,Dateformat 是自 1970 年 1 月 1 日以来经过的天数,POSIXctformat 是自 1970 年 1 月 1 日以来经过的秒数(参考 UTC 时区)。这里有一些例子:
as.numeric(as.Date("1970-01-01", tz="UTC"))\n# 0\nas.numeric(as.Date("1970-01-05", tz="UTC"))\n# 4\nas.numeric(as.POSIXct("1970-01-01 00:00:00", tz="UTC"))\n# 0\nas.numeric(as.POSIXct("1970-01-05 00:00:00", tz="UTC"))\n# 345600\nRun Code Online (Sandbox Code Playgroud)\n\n处理问题的一种方法是将日期转换为数字格式,对数字数据运行回归,然后最后转换回日期。
\n\n在下面的代码中,我假设x$day以格式开始POSIXct。
# Convert POSIXct date to a numeric value equal to number of days since Jan 1, 1970\nx$day.numeric = as.numeric(x$day)/(24*60*60)\n\n# Regression model\nq <- lm(day.numeric ~ sum, data=x)\n\nsummary(q)\n\nCall:\n lm(formula = day.numeric ~ sum, data = x)\n\nResiduals:\n Min 1Q Median 3Q Max \n-22.253 -10.253 1.747 9.994 20.994 \n\nCoefficients:\n Estimate Std. Error t value Pr(>|t|) \n(Intercept) 1.653e+04 8.448e+00 1956.996 < 2e-16 ***\n sum 3.584e-02 9.550e-03 3.753 0.00048 ***\n ---\n Signif. codes: 0 \xe2\x80\x98***\xe2\x80\x99 0.001 \xe2\x80\x98**\xe2\x80\x99 0.01 \xe2\x80\x98*\xe2\x80\x99 0.05 \xe2\x80\x98.\xe2\x80\x99 0.1 \xe2\x80\x98 \xe2\x80\x99 1\n\nResidual standard error: 12.67 on 47 degrees of freedom\nMultiple R-squared: 0.2306, Adjusted R-squared: 0.2142 \nF-statistic: 14.09 on 1 and 47 DF, p-value: 0.0004796\n\n# Predict date at which sum = 6000. Use as.Date to convert numeric value\n# back to a date based on the number of days since Jan 1, 1970.\nas.Date(predict(q, data.frame(sum=6000)), origin="1970-01-01")\n 1 \n"2015-11-08"\nRun Code Online (Sandbox Code Playgroud)\n