我正在寻找一个可以输入自变量和因变量的函数,然后它将返回每个自变量的回归摘要和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
我想创建一个看起来像这样的函数并返回相同的东西:
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。
答案 0 :(得分:0)
将我的独立变量和因变量设置为输入,我在函数中使用一些命令来返回所需的输出。 独立=&#39; Sepal.Width:Petal.Length + Petal.Width&#39; 依赖=&#39; Sepal.Length&#39;
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)
}
然后返回:
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