wrek
wrek

Reputation: 1161

R categorical variable in Linear Regression

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) ?

Upvotes: 7

Views: 1120

Answers (1)

Varnith Chordia
Varnith Chordia

Reputation: 59

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.

Upvotes: 1

Related Questions