R categorical variable in Linear Regression
Asked Answered
F

1

7

I want to fit a Linear Regression in R to a categorical variable that have 3 levels. In particular, my data is the following:

Y = 1, X= "Type 1", A=0.5

Y = 2, X= "Type 2", A=0.3

Y =0.5,X= "Type 3", A=2

Do I simply do the following:

lm(Y~ X+ A) ?

Foresee answered 25/4, 2017 at 17:23 Comment(2)
I would suggest introducing 3 dummy variables (for each type 0-1). Or if they are nominal you can model them as 1,2,3.Regarding
Yes, lm(Y ~ X + A) will work fine. If X isn't already a factor but is a vector of strings, lm will treat it as a factor, using the first value as the reference category and including a dummy variable for each other level in the model. And if X is a factor, fine.Horrify
Y
1

Convert X into factor and then use lm(Y ~ X + A).Or you can use dummyvars from the caret package -

dummy_train<-dummyVars(" ~ .",data=<insert_data_name>)
dummy_train<-data.frame(predict(dummy_train,newdata=<insert_the_same_data_name>))

You can run a regression on this.

Yuu answered 25/4, 2017 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.