简体   繁体   中英

Error with factors as numeric in linear model

I'm trying to fit a lm() model in R as below for matching:

ps_fit <- lm(formula =vote~ factor(treat_news)+ factor(age)+social_class+religion+political_party,data = Brexit_Modified)

but this gives me the error:

using type = "numeric" with a factor response will be ignored not meaningful for factors

and my data frame

str(Brexit_Modified)
    data.frame':    12369 obs. of  8 variables:
     $ id             : int  1 2 3 4 5 6 7 8 9 10 ...
     $ age            : num  58 71 39 73 58 67 20 68 22 42 ...
     $ vote           : Factor w/ 2 levels "0","1": 2 2 2 2 2 2 1 2 1 2 ...
     $ social_class   : Factor w/ 2 levels "0","1": 2 1 2 2 2 1 2 2 2 1 ...
     $ religion       : Factor w/ 2 levels "0","1": 2 2 1 1 2 1 2 2 1 1 ...
     $ political_party: Factor w/ 2 levels "0","1": 1 2 2 1 1 1 2 1 2 2 ...
     $ watch_TV       : num  4.92 5.73 3.04 5.73 4.92 5.73 1.89 5.73 1.89 3.04 ...
     $ treat_news     : num  1 1 0 1 1 1 0 1 0 0 ...

Since your response only contains two values ("0" and "1"), I think you want to fit a propensity model? You could use glm to fit a logistic regression.

ps_fit <- glm(formula =vote~ factor(treat_news)+ 
  age+social_class+religion+political_party,
 data = Brexit_Modified, family = binomial())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM