我想创建一个这样的函数:
在R中可以使用它作为我发现的一般例子:
fit <- lm(y ~ x1 + x2, data=mydata)
我的数据是:
Ay Ty year
20 11 2009
48 27 2010
35 58 2011
如何才能为我的案子做好准备?
答案 0 :(得分:3)
显然你想要适合一个线性模型。这是通过lm
函数完成的。通常你有更多的数据行,然后系数适合,但它会起作用:
> d <- read.table(header=TRUE, text="Ay Ty year
+ 20 11 2009
+ 48 27 2010
+ 35 58 2011")
> d
Ay Ty year
1 20 11 2009
2 48 27 2010
3 35 58 2011
> model <- lm(Ay ~ Ty + year, data = d)
> summary(model)
Call:
lm(formula = Ay ~ Ty + year, data = d)
Residuals:
ALL 3 residuals are 0: no residual degrees of freedom!
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.441e+05 NA NA NA
Ty -2.733e+00 NA NA NA
year 7.173e+01 NA NA NA
Residual standard error: NaN on 0 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: NaN
F-statistic: NaN on 2 and 0 DF, p-value: NA
这将离开你
Ay = -144100 - 2.733 * Ty + 71.73 * year + error
因为残差的平方最小。