使用 lm() 确定 R 拟合的优度

发布于 2024-11-30 20:02:05 字数 741 浏览 1 评论 0原文

我学会了在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦开始←不甜 2024-12-07 20:02:05

如果您想直接访问 summary 生成的片段,您只需调用 summary 并将结果存储在变量中,然后检查结果对象:

rs <- summary(lm1)
names(rs)

也许 rs $sigma 是您要找的吗?

编辑

在有人责备我之前,我应该指出,对于其中一些信息,这不是推荐的访问方式。相反,您应该使用指定的提取器,例如 residuals()coef

If you want to access the pieces produced by summary directly, you can just call summary and store the result in a variable and then inspect the resulting object:

rs <- summary(lm1)
names(rs)

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() or coef.

臻嫒无言 2024-12-07 20:02:05

此代码将执行类似的操作:

 y2 <- seq(1, 11, by=2)+rnorm(6)  # six data points to your four points
 x2=1:6
 lm(y2 ~ x2)
 summary(lm(y2 ~ x2))

调整后的 R^2 是“拟合优度”度量。也就是说,y2 中 99% 的方差可以通过 y2 与 x2 的直线拟合来“解释”。您是否想根据该结果仅用 4 个数据点来解释您的模型取决于判断。这对我来说似乎有些危险。

要提取您使用的残差平方和:

summary(lm(y2~x2))$sigma

请参阅以下内容以获取更多详细信息:

?summary.lm

This code would do something similar:

 y2 <- seq(1, 11, by=2)+rnorm(6)  # six data points to your four points
 x2=1:6
 lm(y2 ~ x2)
 summary(lm(y2 ~ x2))

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:

summary(lm(y2~x2))$sigma

See this for further details:

?summary.lm
尐偏执 2024-12-07 20:02:05

您可以查看一些不错的回归诊断图,其中

plot(YourRegression, which=1:6)

which=1:6 给出了所有六个图。 RESET 测试和 bptest 将测试错误指定和异方差性:

resettest(...)
bptest(...)

有很多资源可以考虑这类事情。 R 中的拟合分布 就是其中之一,并且Faraway 的《实用回归和方差分析》是 R 经典。我基本上是从 Farnsworth 的论文/书中学习了 R 中的计量经济学,尽管我不记得他是否有关于拟合优度的任何内容。

如果您打算在 R 中学习大量计量经济学,R 中的应用计量经济学 是一本很棒的付费书。我经常使用R for Economists网页。

这些是首先浮现在脑海中的。我会再考虑一下。

There are some nice regression diagnostic plots you can look at with

plot(YourRegression, which=1:6)

where which=1:6 give you all six plots. The RESET test and bptest will test for misspecification and heteroskedasticity:

resettest(...)
bptest(...)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文