Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 928
wrek Avatar asked Apr 25 '17 17:04

wrek


People also ask

Can you use categorical variables in linear regression in R?

When building linear model, there are different ways to encode categorical variables, known as contrast coding systems. The default option in R is to use the first level of the factor as a reference and interpret the remaining levels relative to this level.

Can you do regression with only categorical variables?

Is it possible to conduct a regression if all dependent and independent variables are categorical variables? It's certainly possible, even for common or garden regression, so long as the response (dependent) variable is be treated purely numerically.

Can GLM handle categorical variables?

9.4. 1 What is the GLM? The General Linear Model (GLM) is a general mathematical framework for expressing relationships among variables that can express or test linear relationships between a numerical dependent variable and any combination of categorical or continuous independent variables.


1 Answers

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.

like image 62
Varnith Chordia Avatar answered Oct 05 '22 23:10

Varnith Chordia