R programming glm function

+1 vote

I'm trying to run a regression model. When I create it using the glm function like this:

lm.fit <- glm(res~., data=train) #res is class of my dataset

I get an error like this:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

my data set has15 variable, 10 of them are integer and 5 of them are factor.

Jan 23, 2019 in Data Analytics by Sophie may
• 10,620 points
1,732 views

1 answer to this question.

0 votes

Since you mentioned you have factorized variables, check which one of your factors has less than 2 levels.

sapply(train, function(x) if (is.factor(x)) length(levels(x)) else NA)

This should do it.

answered Jan 23, 2019 by Tyrion anex
• 8,700 points

Related Questions In Data Analytics

+3 votes
2 answers

Error: could not find function - R Programming

Yes, Just like @Maverik said, It happens ...READ MORE

answered Aug 23, 2019 in Data Analytics by anonymous
• 33,030 points
33,712 views
0 votes
1 answer

Report function outputs in R programming

In your case, you've stored the output ...READ MORE

answered Jan 14, 2019 in Data Analytics by Sophie may
• 10,620 points
570 views
0 votes
2 answers

R function for finding the index of an element in a vector?

The function match works on vectors : x <- sample(1:10) x # ...READ MORE

answered Dec 12, 2020 in Data Analytics by Rajiv
• 8,870 points
56,641 views
+1 vote
2 answers

Which function can I use to clear the console in R and RStudio ?

Description                   Windows & Linux           Mac Clear console                      Ctrl+L ...READ MORE

answered Apr 17, 2018 in Data Analytics by anonymous
82,365 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,640 views
+1 vote
2 answers
0 votes
1 answer

R programming logic

Use gsub to match the substring that we want ...READ MORE

answered Nov 16, 2018 in Data Analytics by Maverick
• 10,840 points
742 views
0 votes
1 answer
0 votes
1 answer

R programming: How to pass variables from a r program to mysql function?

To include the R variables called start.date and end.date, you can use paste to ...READ MORE

answered Dec 28, 2018 in Data Analytics by Tyrion anex
• 8,700 points
1,336 views
0 votes
1 answer

R programming: sapply() function

There are 2 ways to do it, ...READ MORE

answered Jan 23, 2019 in Data Analytics by Tyrion anex
• 8,700 points
444 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP