r-从逐步变量过程中提取重要变量,并将其包含在数据表中



我需要从逐步(两个方向)变量选择过程中找到重要变量,并在数据表中包含以进行数据提取。

iris$area <- iris$Petal.Length * iris$Petal.Width
iris <- data.table(iris)
mydata <- iris[Species %in% "virginica", list(Sepal.Length,Sepal.Width,Petal.Length,Petal.Width,area)]
fit <- lm(area~., data=mydata)
satis.step <- step(fit, direction="both")
datanew <- iris[Species %in% "virginica", list(Species, satis.step)]
    Output:
(Let's assume step wise regression selects Sepal.Length and Sepal.Width so it has the values for the those two predictors)
 datanew <- iris[Species %in% "virginica", list(Species, Sepal.Length,Sepal.Width)] 

我很难在R设想中设置此设置。任何帮助都将受到赞赏。

预先感谢

我认为这应该有效。

datanew <- data[union(c(YEAR, RANGE, ID, COUNTY, LR),
                      attr(satis.step$formula, "term.labels"))]

工作示例:

m1 = lm(Sepal.Length ~ ., data = iris)
m2 = step(m1)
attr(m2$formula, "term.labels")
# [1] "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"  

union应照顾任何重复。

最新更新