0

I am trying to run a logistic regression using the lme4 package in Program R using the glmer function.

However, every time I run the regression I get a error:

Error in eval(family$initialize, rho) : y values must be 0 <= y <= 1

I know this error is telling me that one of my variables need changed, however; all of my variables are numeric, so I am unsure of the issue. Normally this occurs if a variable is a character within the data frame.

Here is my regression code:

speedmod <- glmer(speed ~ HaFP + HaFPWFeed + NumFeeder + (1|trackId), data=befacorn, family=binomial(link=logit))

And here is the head of the dataframe I am using:

    structure(list(trackId = c(100, 100, 13, 13, 17, 17), speed = c(116.999313, 
128.9319019, 164.6205906, 141.6179845, 209.5512926, 159.5864867
), Encamped = c(27.04731971, 26.65969681, 13.82608696, 11.25608907, 
22.45217391, 32.26981099), Constrained = c(48.36738257, 44.71162223, 
54.33043478, 60.17745303, 40.48695652, 34.76677649), Unconstrained = c(24.58529771, 
28.62868095, 31.84347826, 28.5664579, 37.06086957, 32.96341252
), dist = c(33.47023812, 30.73586278, 40.05010243, 36.29203072, 
46.60945598, 39.90154752), NDP = c(6812.840821, 1454.308521, 
959.0027313, 971.1027189, 766.8727274, 654.5480648), area = c(1212.470847, 
58.93086937, 125.1207848, 63.17518578, 439.2532168, 182.1664135
), HaFP = c(45.05, 2.2, 12.49, 10.2, 9.6, 3.1), HaFPWFeed = c(7.58, 
2.2, 9.32, 9.3, 0, 0), NumFeeder = c(14, 5, 1, 0, 1, 1)), row.names = c(NA, 
6L), class = "data.frame")
Colby
  • 27
  • 9
  • 1
    Do you know what your y-variable is? – Hong Ooi Oct 23 '20 at 20:02
  • 3
    Notice that `speed` is a continuous variable, `speed = c(116.999313, 128.9319019, 164.6205906, 141.6179845, 209.5512926, 159.5864867 )`, consider a linear regression in this case. – Miguel Trejo Oct 23 '20 at 20:12
  • @MiguelTrejo is right. You can run linear regression if your target variable is numeric. If the target variable is a class that contains distinct values then you can apply logistic regression on that data. https://stackoverflow.com/questions/12146914/what-is-the-difference-between-linear-regression-and-logistic-regression – Regressor Oct 23 '20 at 20:39
  • perhaps you're trying to fit a nonlinear mixed-effects model? e.g. see https://asancpt.github.io/nlme/chapter-6.html – Ben Bolker Oct 23 '20 at 20:55
  • Thank you, @MiguelTrejo. It's always something simple that is overlooked. – Colby Oct 23 '20 at 21:00

2 Answers2

1

The error code y values must be 0 <= y <= 1 is telling you that the response variable (or y) must be between 0 and 1. This is because you have selected family=binomial, which is for binary data (0,1). Logistic regression is another name for this type of regression. As @MiguelTrejo says in the comments, your response variable in your equation glmer(response ~ predictor1 + predictor2...) is continuous.

Before selecting a linear model in this case, be sure you know why you are picking a family for your model: see https://stats.stackexchange.com/questions/190763/how-to-decide-which-glm-family-to-use and http://bbolker.github.io/mixedmodels-misc/glmmFAQ.html for example. The interpretation of the output is different depending on the link function (which often changes with the family).

Additionally, your structure + (1|trackId) indicates a random intercept, see https://stats.stackexchange.com/questions/31569/questions-about-how-random-effects-are-specified-in-lmer for help in coding random effects and what it means.

Dylan_Gomes
  • 2,066
  • 14
  • 29
  • actually my comment was not intended to be primarily about the random effects part of the model, but that the OP *might* have intended to fit a nonlinear (mixed) least-squares model, i.e. `response ~ Normal(mu, sigma); mu = logist(r,K,x0)` – Ben Bolker Oct 23 '20 at 22:06
  • Ah, I see. Apologies. I have edited that out, but left the content. – Dylan_Gomes Oct 23 '20 at 23:13
0

This error can be solved by using as.factor(speed):

speedmod <- glmer(as.factor(speed) ~ HaFP + HaFPWFeed + NumFeeder + (1|trackId), data=befacorn, family=binomial(link=logit))