R categorical variable in linear regression

I want to fit linear regression in R to a categorical variable that has 3 levels. In particular, my data is as follows:

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

I just do the following:

lm(Y~ X+ A) ?

+5
source share
1 answer

Convert X to coefficient, and then use lm (Y ~ X + A). Or you can use dummyvars from 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.

+1
source

Source: https://habr.com/ru/post/1267152/


All Articles