R 在线性回归模型中包含误差项
我想知道是否有一种方法可以包含线性回归模型的误差项,如:R 在线性回归模型中包含误差项,r,linear-regression,R,Linear Regression,我想知道是否有一种方法可以包含线性回归模型的误差项,如: r = lm(y ~ x1+x2) 代码r=lm(y~x1+x2)意味着我们将y建模为x1和x2的线性函数。由于模型并不完美,因此会有一个剩余项(即模型未能拟合的剩余项) 在数学方面,正如Rob Hyndman在评论中指出的,y=a+b1*x1+b2*x2+e,其中a,b1和b2是常数,e是残差(假定为正态分布) 看一个具体的例子,考虑R.附带的IRIS数据。 model1 <- lm(Sepal.Length ~ Sepal.W
r = lm(y ~ x1+x2)
代码r=lm(y~x1+x2)
意味着我们将y建模为x1和x2的线性函数。由于模型并不完美,因此会有一个剩余项(即模型未能拟合的剩余项)
在数学方面,正如Rob Hyndman在评论中指出的,y=a+b1*x1+b2*x2+e
,其中a
,b1
和b2
是常数,e
是残差(假定为正态分布)
看一个具体的例子,考虑R.
附带的IRIS数据。model1 <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data=iris)
对模型中使用的每一行数据计算了残差
> residuals(model1)
1 2 3 4 5
0.0845842387 0.2100028184 -0.0492514176 -0.2259940935 -0.0804994772
# etc. There are 150 residuals and 150 rows in the iris dataset.
(编辑:将摘要信息剪切为不相关。)
编辑: 您在中的注释中提到的
错误
值将在aov的帮助页面上解释
If the formula contains a single ‘Error’ term, this is used to
specify error strata, and appropriate models are fitted within
each error stratum.
比较以下内容(改编自?aov
页面。)
你所说的错误术语是什么意思?您的代码将符合以下线性模型:y=a+b1*x1+b2*x2+e,其中e是错误。那么模型已经包含错误项了吗?但是我也看到了:y~A*B+错误(C)嘿,里奇,这看起来很有趣。所以我想我可以在R中试试这个:>v=aov(收益率~NPK,NPK)>v2=aov(收益率~NPK+误差(块),NPK)>系数(v)>系数(v2)这是什么类型的回归?那么像N1:P1和N1:P1:K1这样的系数是什么?@phpdash:这有时被称为分裂图方差分析。这里有一个由迈克尔·克劳利(Michael Crawley)编写的统计计算的逐步示例。
If the formula contains a single ‘Error’ term, this is used to
specify error strata, and appropriate models are fitted within
each error stratum.
> utils::data(npk, package="MASS")
> aov(yield ~ N*P*K, npk)
Call:
aov(formula = yield ~ N * P * K, data = npk)
Terms:
N P K N:P N:K P:K N:P:K Residuals
Sum of Squares 189.2817 8.4017 95.2017 21.2817 33.1350 0.4817 37.0017 491.5800
Deg. of Freedom 1 1 1 1 1 1 1 16
Residual standard error: 5.542901
Estimated effects may be unbalanced
> aov(yield ~ N*P*K + Error(block), npk)
Call:
aov(formula = yield ~ N * P * K + Error(block), data = npk)
Grand Mean: 54.875
Stratum 1: block
Terms:
N:P:K Residuals
Sum of Squares 37.00167 306.29333
Deg. of Freedom 1 4
Residual standard error: 8.750619
Estimated effects are balanced
Stratum 2: Within
Terms:
N P K N:P N:K P:K Residuals
Sum of Squares 189.28167 8.40167 95.20167 21.28167 33.13500 0.48167 185.28667
Deg. of Freedom 1 1 1 1 1 1 12
Residual standard error: 3.929447
1 out of 7 effects not estimable
Estimated effects may be unbalanced