使用lm的R中的线性回归:函数中的不同汇总输出 [英] Linear regression in R using lm: Different summary output in a function

查看:55
本文介绍了使用lm的R中的线性回归:函数中的不同汇总输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关使用lm函数在R中进行线性回归的简要问题.我注意到,将summary命令用作函数的一部分时,输出是不同的.

Brief question regarding linear regression in R using the lm function. I noticed that the output is different when using the summary command as part of a function.

当我输入时:

model1 <- lm (PostVal_Ave ~ Int)
summary(model1)

以下信息在控制台中返回:

The follow is returned in the console:

Call:
lm(formula = PostVal_Ave ~ Int)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.9871 -0.8897  0.4853  1.0129  1.5129 

Coefficients:
        Estimate Std. Error t value Pr(>|t|)
(Intercept)   5.4871     0.1426  38.491   <2e-16
Int           0.2776     0.1988   1.396    0.164

(Intercept) ***
Int            
---
Signif. codes:  
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.322 on 175 degrees of freedom
(35 observations deleted due to missingness)
Multiple R-squared:  0.01102,   Adjusted R-squared:  0.005366 
F-statistic: 1.949 on 1 and 175 DF,  p-value: 0.1644

但是,当编写一个函数以便能够为多个模型生成输出并能够为多个因变量生成结果时,我输入:

But, when writing a function in order to be able to produce output for multiple models and to be able to produce results for multiple dependent variables, I enter:

allModels <- function(x){
    model2 <- lm (x ~ Int)
    model2.1 <- lm (x ~ Int + cPreEff)
    model2.2 <- lm (x ~ Int + cPreEff + Gender + Grade)
    return(c(summary(model1), summary(model1.1), summary(model1.2)))}

与模型1的输出相比,我得到了相同的输出,但是对于这三个模型(model2,model2.1和model2.2)有很多额外的输出.具体来说,输出包含三个模型中每个模型的每个案例的残差以及有关每个缺少数据的案例的信息.建议将不胜感激.谢谢.

And I get the same output compared to the output for model 1, but with a lot of additional output for these three models (model2, model2.1, and model2.2). Specifically, the output contains the residuals for each case for each of the three models and information about every case with missing data. Advice would be much appreciated. Thanks.

推荐答案

请注意, lm()在其上返回类"lm"和 summary()的对象对象产生一个"summary.lm"对象.有自定义的 print.lm() print.summary.lm()对象.因此,打印到控制台的内容可能与对象本身的内容不同.

Note that lm() returns an object of class "lm" and summary() on that object produces a "summary.lm" object. There are custom print.lm() and print.summary.lm() objects. So what ever is printed to the console may be different than what's in the object itself.

当您手动连接( c())两个summary.lm对象时,将创建一个聚合列表并丢失适当的类.您可能想改为返回对象列表

When you manually concatenate (c()) two summary.lm objects, you create one aggregated list and lose the proper class. You probably want to return a list of objects instead

return(list(summary(model1), summary(model1.1), summary(model1.2)))

这篇关于使用lm的R中的线性回归:函数中的不同汇总输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆