[英]How to generate observations from a linear model in R
我一直在嘗試驗證在通常的假設下OLS估計量是否一致。
您能否告訴我如何從線性模型中生成一些觀測值,以便以后可以對該數據進行回歸分析並希望驗證OLS的理想特性?
先感謝您,
不清楚這是否是您想要的嗎?
# sample data: x1 and x2 uncorrelated
df <- data.frame(x1=sample(1:100,100),x2=sample(1:100,100))
# y = 1 +2.5*x1 - 3.2*x2 + N(0,5)
df$y <- with(df,1 + 2.5*x1 -3.2*x2 + rnorm(100,0,5))
fit <- lm(y~x1+x2, data=df)
summary(fit)
#...
# Residuals:
# Min 1Q Median 3Q Max
# -9.8951 -2.6056 -0.4384 3.6082 9.5044
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 1.954 1.263 1.548 0.125
# x1 2.516 0.016 157.257 <2e-16 ***
# x2 -3.237 0.016 -202.306 <2e-16 ***
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# Residual standard error: 4.611 on 97 degrees of freedom
# Multiple R-squared: 0.9986, Adjusted R-squared: 0.9986
# F-statistic: 3.48e+04 on 2 and 97 DF, p-value: < 2.2e-16
請注意,se〜4.6與“ true”的se = 5一致。還請注意,由於se(y | x)= 5,所以(Intercept)的估算很差。
par(mfrow=c(2,2))
plot(fit)
請注意,QQ圖確認正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.