繁体   English   中英

如何在 r 中进行逐步回归以获得更多的自变量和更少的观察值?

[英]How to do stepwise regression in r for more independent variables and less observations?

我正在尝试对以下数据进行逐步回归:

y <- c(1.2748, 1.2574, 1.5571, 1.4178, 0.8491, 1.3606, 1.4747, 1.3177, 1.2896, 0.8453)
x <- data.frame(A = c(2,3,4,5,6,2,3,4,5,6), 
                B = c(2,4,1,3,5,1,3,5,2,4)*100, 
                C = c(9,5,11,5,11,7,13,7,13,9), 
                D = c(6,5,3,7,6,4,3,7,5,4), 
                E = c(1,1,0.8,0.8,0.6,0.6,0.4,0.4,0.2,0.2))
x$A2 <- x$A^2
x$B2 <- x$B^2
x$C2 <- x$C^2
x$D2 <- x$D^2
x$E2 <- x$E^2
x$AB <- x$A*x$B

正如我们所见,它有 10 个观测值和 11 个自变量,因此我无法为其构建线性回归 model。 事实上,只有少数几个因素是有用的,在这种情况下,我需要使用逐步回归和“向前”将自变量添加到我的公式中。 但是stats::step function不能用。 我想知道是否有办法做到这一点。 我知道有一个名为“StepReg”的 package,但我不完全了解如何使用它以及如何读取结果。 谢谢!

我只是使用您使用 R package StepReg提供的数据运行逐步回归

这是代码,希望这可以帮助你。

library(StepReg)
df <- data.frame(y,x)
# forward method with information criterion 'AIC', you can choose other information criterion
stepwise(df, y="y", exclude=NULL, include=NULL, Class=NULL,
         selection="forward", select="AIC")

# forward method with significant levels, significant level for entry = 0.15
stepwise(df, y="y", exclude=NULL, include=NULL, Class=NULL,
         selection="forward", select="SL",sle=0.15)

您可以使用olsrr package,它给出与 SPSS 类似的结果。 这是解决方案

library(olsrr)
df <- data.frame(y, x)
model <- lm(y ~ ., data = df)
smlr <- ols_step_both_p(model, pent = 0.05, prem = 0.1) #pent p value; variables with p value less than pent will enter into the model.
#premp value; variables with p more than prem will be removed from the model. 

您可以拨打model获取详细信息

smlr
smlr$model
smlr$beta_pval #regression coefficients with p-values

我将pentprem的值保持为与 SPSS 使用的默认值相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM