打印回归摘要和 5 数字摘要的功能

Function to print out regression summary and 5 number summary

我正在寻找一个可以输入自变量和因变量的函数,然后它将 return 每个自变量的回归摘要和 5 数字摘要。这是一个示例和我的设置:

attach(iris)
five_num=matrix(0,nrow=3,ncol=6)
rownames(five_num)=c('Sepal.Width','Petal.Length','Petal.Width')
colnames(five_num)=c('Min','1st Qu','Median','Mean','3rd Qu','Max')
for (i in 1:3){
  five_num[i,]=summary(eval(parse(text=rownames(five_num)[i])))
}

然后我只打印回归和 5 个数字摘要:

summary(lm(Sepal.Length~Sepal.Width+Petal.Length+Petal.Width,data=iris))
Call:
lm(formula = Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, 
    data = iris)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.82816 -0.21989  0.01875  0.19709  0.84570 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)   1.85600    0.25078   7.401 9.85e-12 ***
Sepal.Width   0.65084    0.06665   9.765  < 2e-16 ***
Petal.Length  0.70913    0.05672  12.502  < 2e-16 ***
Petal.Width  -0.55648    0.12755  -4.363 2.41e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3145 on 146 degrees of freedom
Multiple R-squared:  0.8586,    Adjusted R-squared:  0.8557 
F-statistic: 295.5 on 3 and 146 DF,  p-value: < 2.2e-16



five_num
 Min 1st Qu Median  Mean 3rd Qu Max
Sepal.Width  2.0    2.8   3.00 3.057    3.3 4.4
Petal.Length 1.0    1.6   4.35 3.758    5.1 6.9
Petal.Width  0.1    0.3   1.30 1.199    1.8 2.5

我想制作一个看起来像这样的函数并且 return 同样的事情:

reg_5_num=function(dependent,independent){
code here
}

我 运行 遇到的主要问题是,当我标记我的自变量时,我不能 运行 它们进入回归,因为它需要加号才能工作。

此外,我希望函数也能使用交互项。如果我的回归是

summary(lm(Sepal.Length~Sepal.Width:Petal.Length+Petal.Width,data=iris))

Call:
lm(formula = Sepal.Length ~ Sepal.Width:Petal.Length + Petal.Width, 
    data = iris)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.80414 -0.24478 -0.02936  0.25741  0.94391 

Coefficients:
                         Estimate Std. Error t value Pr(>|t|)    
(Intercept)               4.14976    0.07771  53.397  < 2e-16 ***
Petal.Width              -0.31056    0.11365  -2.733  0.00705 ** 
Sepal.Width:Petal.Length  0.18510    0.01654  11.191  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3524 on 147 degrees of freedom
Multiple R-squared:  0.8213,    Adjusted R-squared:  0.8189 
F-statistic: 337.8 on 2 and 147 DF,  p-value: < 2.2e-16

我仍然希望看到相同的 five_num。

将我的自变量和因变量设置为输入,我在我的函数中使用一些命令来 return 所需的输出。 独立='Sepal.Width:Petal.Length+Petal.Width' 依赖='Sepal.Length'

regs=function(independent,dependent) {
summary(lm(as.formula(paste(c(dependent,independent),collapse='~'))))
vars=unlist(strsplit(independent, "[:+]"))
five_num=matrix(0,nrow=length(vars),ncol=6)
rownames(five_num)=vars
colnames(five_num)=c('Min','1st Qu','Median','Mean','3rd Qu','Max')
for (i in 1:length(vars)){
  five_num[i,]=summary(eval(parse(text=vars[i])))
}
print(summary(lm(as.formula(paste(c(dependent,independent),collapse='~')))))
print(five_num)
}

然后 returns:

regs(independent,dependent)

Call:
lm(formula = as.formula(paste(c(dependent, independent), collapse = "~")))

Residuals:
     Min       1Q   Median       3Q      Max 
-0.80414 -0.24478 -0.02936  0.25741  0.94391 

Coefficients:
                         Estimate Std. Error t value Pr(>|t|)    
(Intercept)               4.14976    0.07771  53.397  < 2e-16 ***
Petal.Width              -0.31056    0.11365  -2.733  0.00705 ** 
Sepal.Width:Petal.Length  0.18510    0.01654  11.191  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3524 on 147 degrees of freedom
Multiple R-squared:  0.8213,    Adjusted R-squared:  0.8189 
F-statistic: 337.8 on 2 and 147 DF,  p-value: < 2.2e-16

             Min 1st Qu Median  Mean 3rd Qu Max
Sepal.Width  2.0    2.8   3.00 3.057    3.3 4.4
Petal.Length 1.0    1.6   4.35 3.758    5.1 6.9
Petal.Width  0.1    0.3   1.30 1.199    1.8 2.5