regarding handling many binary independent variables in lm

孤街浪徒 提交于 2019-12-11 05:23:43

问题


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.


回答1:


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

str(mydata)

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.


来源:https://stackoverflow.com/questions/26822526/regarding-handling-many-binary-independent-variables-in-lm

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