从 R 中的逐步回归导入列名 [英] Import column names from step wise regression in R

查看:47
本文介绍了从 R 中的逐步回归导入列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想导入从逐步回归过程中选择的变量作为列名,以便输​​出"具有逐步回归中的变量,如下所示.但是,我下面的代码没有这样做.

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 = ", ")) ]

输出

我需要输出看起来像:

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

推荐答案

我明白了.

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

这篇关于从 R 中的逐步回归导入列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆