Linear regression in R with if statement [duplicate]

醉酒当歌 提交于 2019-11-28 14:30:57

If looks like there are a few issues here. First, you've stored all your data in separate vectors rent, income and black. You should instead store it in a data frame:

data <- data.frame(rent, income, black)

To limit a data frame based on a logical expression, you can use the subset function:

data.limited <- subset(data, black == 1)

Finally, you can run your analysis on your limited data frame (presumably without the black variable):

model3 <- lm(rent~I(income^2)+income, data=data.limited)

Why not subset the data before running the model? I personally prefer using a dataframe rather than separate vectors which will make the subsetting easier.

df <- data.frame(rent, income, black)

Then subset the dataframe, o create another one

df <- df[df$black==1,]

And run the model

model3 <- lm(rent ~ I(income^2) , data=df)
Isidro Jr

The code written below should do it.

model3 <- lm(rent~I(income^2)+income+black, data=df, subset=df$black==1))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!