问题
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