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)
?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With