Import column names from step wise regression in R

与世无争的帅哥 提交于 2019-12-02 16:55:47

问题


I want to import the variables selected from the step-wise regression process as column names so that I have the "Output" has the variables from the step-wise regression as shown below. But, my code below does NOT do that.

I cannot understand what I am doing wrong here. Can somebody help me please?

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, paste(attr(satis.step$formula, "term.labels"),collapse = ", ")) ]

Output

I need the output to look like:

Species       Sepal.Length     Sepal.Width
------------------------------------------
virginicia         6.3            3.3
virginicia         5.8            2.7

回答1:


I got this.

selvars=c("Species", attr(satis.step$terms, "term.labels"))
datanew <- iris[Species %in% "virginica", ..selvars ]

> datanew
      Species Sepal.Length Petal.Length Petal.Width
 1: virginica          6.3          6.0         2.5
 2: virginica          5.8          5.1         1.9
 3: virginica          7.1          5.9         2.1


来源:https://stackoverflow.com/questions/46576491/import-column-names-from-step-wise-regression-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!