使用跳转库。绘制变量时,y轴显示已调整R ^ 2。您查看框在最高R ^ 2处的黑色位置。这将显示您应该用于多元线性回归的变量。
下面的葡萄酒示例:
library(leaps)
regsubsets.out <-
regsubsets(Price ~ Year + WinterRain + AGST + HarvestRain + Age + FrancePop,
data = wine,
nbest = 1, # 1 best model for each number of predictors
nvmax = NULL, # NULL for no limit on number of variables
force.in = NULL, force.out = NULL,
method = "exhaustive")
regsubsets.out
#----When you plot wherever R^2 is the highest with black boxes,
#so in our case AGST + HarvestRain + WinterRain + Age and the dependent var.is Price----#
summary.out <- summary(regsubsets.out)
as.data.frame(summary.out$outmat)
plot(regsubsets.out, scale = "adjr2", main = "Adjusted R^2")