使用 lm() 确定 R 拟合的优度
我学会了在 R 脚本中使用 lm 与某些点进行线性拟合。所以,我这样做了(效果很好),并打印出了合身性:
lm(formula = y2 ~ x2)
Residuals:
1 2 3 4
5.000e+00 -1.000e+01 5.000e+00 7.327e-15
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 70.000 17.958 3.898 0.05996 .
x2 85.000 3.873 21.947 0.00207 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 8.66 on 2 degrees of freedom
Multiple R-squared: 0.9959, Adjusted R-squared: 0.9938
F-statistic: 481.7 on 1 and 2 DF, p-value: 0.00207
我正在尝试确定判断这种合身性的最佳方法。我需要将这种拟合与其他一些拟合(使用 lm() 函数也是线性的)进行比较。该摘要的哪些值是判断这种拟合程度的最佳方法?我正在考虑使用残留标准误差。任何建议。另外,如何从 fit 变量中提取该值?
I learned to get a linear fit with some points using lm in my R script. So, I did that (which worked nice), and printed out the fit:
lm(formula = y2 ~ x2)
Residuals:
1 2 3 4
5.000e+00 -1.000e+01 5.000e+00 7.327e-15
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 70.000 17.958 3.898 0.05996 .
x2 85.000 3.873 21.947 0.00207 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 8.66 on 2 degrees of freedom
Multiple R-squared: 0.9959, Adjusted R-squared: 0.9938
F-statistic: 481.7 on 1 and 2 DF, p-value: 0.00207
I'm trying to determine the best way to judge how great this fit is. I need to compare this fit with a few others (which are also linear using lm()
function). What value from this summary would be the best way to judge how good this fit is? I was thinking to use the residual standard error. Any suggestions. Also, how do I extract that value from the fit variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想直接访问
summary
生成的片段,您只需调用summary
并将结果存储在变量中,然后检查结果对象:也许
rs $sigma
是您要找的吗?编辑
在有人责备我之前,我应该指出,对于其中一些信息,这不是推荐的访问方式。相反,您应该使用指定的提取器,例如
residuals()
或coef
。If you want to access the pieces produced by
summary
directly, you can just callsummary
and store the result in a variable and then inspect the resulting object:Perhaps
rs$sigma
is what you're looking for?EDIT
Before someone chides me, I should point out that for some of this information, this is not the recommended way to access it. Rather you should use the designated extractors like
residuals()
orcoef
.此代码将执行类似的操作:
调整后的 R^2 是“拟合优度”度量。也就是说,y2 中 99% 的方差可以通过 y2 与 x2 的直线拟合来“解释”。您是否想根据该结果仅用 4 个数据点来解释您的模型取决于判断。这对我来说似乎有些危险。
要提取您使用的残差平方和:
请参阅以下内容以获取更多详细信息:
This code would do something similar:
The adjusted R^2 is the "goodness of fit" measure. It is saying that 99% of the variance in y2 can be "explained" by a straight line fit of y2 to x2. Whether you want to interpret your model with only 4 data points on the basis of that result is a matter of judgment. It would seem to somewhat dangerous to me.
To extract the residual sum of squares you use:
See this for further details:
您可以查看一些不错的回归诊断图,其中
which=1:6 给出了所有六个图。 RESET 测试和 bptest 将测试错误指定和异方差性:
有很多资源可以考虑这类事情。 R 中的拟合分布 就是其中之一,并且Faraway 的《实用回归和方差分析》是 R 经典。我基本上是从 Farnsworth 的论文/书中学习了 R 中的计量经济学,尽管我不记得他是否有关于拟合优度的任何内容。
如果您打算在 R 中学习大量计量经济学,R 中的应用计量经济学 是一本很棒的付费书。我经常使用R for Economists网页。
这些是首先浮现在脑海中的。我会再考虑一下。
There are some nice regression diagnostic plots you can look at with
where which=1:6 give you all six plots. The RESET test and bptest will test for misspecification and heteroskedasticity:
There are a lot of resources out there to think about this sort of thing. Fitting Distributions in R is one of them, and Faraway's "Practical Regression and Anova" is an R classic. I basically learned econometrics in R from Farnsworth's paper/book, although I don't recall if he has anything about goodness of fit.
If you are going to do a lot of econometrics in R, Applied Econometrics in R is a great pay-for book. And I've used the R for Economists webpage a lot.
Those are the first ones that pop to mind. I will mull a little more.