我想對一組變量運行最佳子集迴歸,然後使用R獲得最佳3個變量。我在獲取最佳2個變量時遇到了問題。我已經在下面包含了我的代碼。從最佳子集中獲取最佳變量回歸R
set.seed(10)
a <- 1:100
b <- 1:100
c <- 1:100
d <- 1:100
e <- 1:100
f <- 1:100
g <- 1:100
h <- 1:100
data <- data.frame(a, b, c, d, e, f, g, h)
library(leaps)
# best subsets regression
test <- regsubsets(a ~ b + c + d + e + f + g + h, data=data, nbest=4)
# nbest = 4, is the number of subsets of each size that is reported
# plot a table of models showing variables in each model.
summary(test)
# models are ordered by the selection statistic.
plot(test,scale="r2")
#get the variables that are important to the model
coef(test, 2)
#NOTE: THIS DOESN'T GIVE ME THE 2 BEST VARIABLES. IT ONLY GIVES ME THE BEST VARIABLE AT THE 2ND ITERATION. LOOK AT:
coef(test, 1:2)
您的幫助將不勝感激!
最佳, 達納
我對你們的例子有點困惑,因爲所有的變量都完全相同。 –
您可能想要提供真實的數據。根據您當前的數據,由於所有數據都是相同的,如果所有潛在模型包含相同數量的變量,它們將具有相同的擬合優度。這可能是你的問題。 –