本文介绍了关于在lm中处理许多二进制自变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用lm构建线性回归模型时,数据集包含约20个独立变量.我是否需要明确地将它们解释为factor?如果必须的话,我该怎么做?一一声明很繁琐.

When building the linear regression model using lm, the data set has about 20 independent variables. Do I need to explicitly clarify them as factor? If I have to, how can I do that? It can be very tedious to declare one by one.

推荐答案

首先,使用Commande检查哪些变量R已自动转换为因子

First, check which variables R has automatically converted into factors with the commande

str(mydata)

然后,如果您想轻松地将多个变量转换为因子,则可以执行以下操作:创建一个"mycol"变量,其中包含要转化为因子的列数

Then if you want to convert several variable into factors easily, you can do something like this:create a "mycol" variable with the No of columns you want to turn into factor

mycol <- c(1,4,5,7:15)
mydata[,  mycol] <- lapply(mydata[,  mycol], as.factor) # to turn them into factor var.
mydata[, -mycol] <- lapply(mydata[, -mycol], as.factor) # to turn all the others into factor var.

这篇关于关于在lm中处理许多二进制自变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 03:32