簡體   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