r - Error when building regression model using lm ( Error in `contrasts<-`(`*tmp*`... contrasts can be applied only to factors with 2 or more levels) -


i error depending on variables include , sequence in specify them in formula:

error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isof[nn]]) :    contrasts can applied factors 2 or more levels 

i've done little research on , looks caused variable in question not being factor variable. in case (is_women_owned), factor variable 2 levels ("yes", "no").

> levels(customer_accounts$is_women_owned) [1] "no"  "yes" 

no error:

f1 <- lm(combined_sales ~ is_women_owned, data=customer_accounts) 

no error:

f2 <- lm(combined_sales ~ total_assets + market_value + total_empl + empl_growth + sic + city + revenue_growth + revenue + net_income + income_growth, data=customer_accounts) 

regressing on above formula plus factor variable "is_women_owned":

f3 <- lm(combined_sales ~ total_assets + market_value + total_empl + empl_growth + sic + city + revenue_growth + revenue + net_income + income_growth + is_women_owned, data=customer_accounts)  error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isof[nn]]) :    contrasts can applied factors 2 or more levels 

i same error when applying stepwise linear regression, expect.

this seems bug, should give model "is_women_owned" perhaps offers no additional explanatory value because highly correlated other variables, not error out this.

i verified there no missing data variable, too:

> which(is.na(customer_accounts$is_women_owned)) integer(0) 

also, there 2 values present in factor variable:

customer_accounts$is_women_owned[1:20]  [1] no  no  no  no  no  no  no  no  no  no  no  no  no  no  yes no  [17] no  no  no  no  levels: no yes 

twofac = data.frame("y" = c(1,2,3,4,5,1), "x" = c(2,56,3,5,2,1), "f" = c("apple","apple","apple","apple","apple","banana")) onefac = twofac[1:5,]  lm(y~x+f,data=twofac) lm(y~x+f,data=onefac)  > str(onefac) 'data.frame':   5 obs. of  3 variables:  $ y: num  1 2 3 4 5  $ x: num  2 56 3 5 2  $ f: factor w/ 2 levels "apple","banana": 1 1 1 1 1 > str(twofac) 'data.frame':   6 obs. of  3 variables:  $ y: num  1 2 3 4 5 1  $ x: num  2 56 3 5 2 1  $ f: factor w/ 2 levels "apple","banana": 1 1 1 1 1 2 > lm(y~x+f,data=twofac)  call: lm(formula = y ~ x + f, data = twofac)  coefficients: (intercept)            x      fbanana       3.30783     -0.02263     -2.28519    > lm(y~x+f,data=onefac) error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isof[nn]]) :    contrasts can applied factors 2 or more levels 

if run above notice twofac, model 2-level factor both factors present, run no problem. onefac, model same 2-level factor 1 level present, gives same error got.

if factor has 1 of levels regressing against factor gives no additional information constant across responsevariables


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -