I have this data:
library(tidyverse)
df <- tibble(
"racecmb" = c("White", "White", "White", "White", "White", "White",
"White", "White", "Black", "White", "Mixed",
"Black", "White", "White", "White"),
"age" = c(77,74,55,62,60,59,32,91,75,73,43,67,58,18,57),
"income" = c("10 to under $20,000", "100 to under $150,000",
"75 to under $100,000", "75 to under $100,000",
"10 to under $20,000", "20 to under $30,000",
"100 to under $150,000", "20 to under $30,000",
"100 to under $150,000", "20 to under $30,000",
"100 to under $150,000", "Less than $10,000",
"$150,000 or more", " 30 to under $40,000",
"50 to under $75,000"),
"party" = c("Independent", "Independent", "Independent", "Democrat",
"Independent", "Republican", "Independent",
"Independent", "Democrat", "Republican", "Republican",
"Democrat", "Democrat", "Independent", "Independent"),
"ideology" = c("Moderate", "Moderate", "Conservative", "Moderate",
"Moderate", "Very conservative", "Moderate",
"Conservative",
"Conservative", "Moderate", "Conservative",
"Very conservative", "Liberal", "Moderate", "Conservative")
)
I want (have tried) to run a simple multiple regression:
regression <- lm(party ~ income + ideo + age, data = df) %>%
summary()
I get this error:
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
NA/NaN/Inf in 'y'
My goal is to explain the way some people vote, but I don't see how to effectively code the data for my model.
Any comments/suggestions are appreciated...
So to begin with, using lm() for categorical variables is not ideal. What you're looking to use is either rpart() which will give you output as categories or classes, or you can use multinomial logit/probit regression to return probabilities of outcomes occuring given some conditions.
Packages to install: rpart and statisticalModeling
If you did not have a categorical response variable, you could convert your categorical variables into dummy variables and then run your regression including your dummy variables (remember to leave one as baseline).
This can be quickly achieved using the fastDummies package:
Example: df <- dummy_cols(df, select_columns = "ideology")
If your sample size is considerable, then you may also want to consider interactions in your model between the dummied variables!
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