0

I am using lme4 R package, but I have NA values in data. I am running the command given below (where, Rtot contains some NaN values.)

 event_lme <- lmer(Rtot ~1 + (1 |EQID))

Rtot contains some NaN values.

When I run this it gives me this error :

Error in KhatriRao(sm, t(mm)) : (p <- ncol(X)) == ncol(Y) is not TRUE

I don't want to drop NA values. Ultimately I am looking for ranef(event_lme) which will have NA values at appropriate places.

Here is a subset of my data

Rtot EQID
NA 127
NA 30
0.058011 151
NA 127
0.5780 1135
0.3306 127
First 1135
0.208 1039
0.057 30

Please advise how to resolve this.

Thanks

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Are you sure they are NaN values and not NA values? How do you want to handle that data? Do you just want to drop those rows? – MrFlick Dec 18 '22 at 22:05
  • You should spell package names correctly. "LME4" != "lme4". Also, most R regression functions expect a data parameter value. I would think that especially true in mixed effects modeling. The functions often "clean things up" by removing rows with missing data. Do you get the same error with: `event_lme <- lmer(Rtot ~1 + (1 |EQID), data=eqidssn)`? – IRTFM Dec 19 '22 at 01:50
  • 2
    Here's a reproducible example that doesn't have encounter any problems. `set.seed(101); Rtot <- rnorm(1000); Rtot[1] <- NaN; EQID <- rep(1:20, each = 50); lme4::lmer(Rtot ~ 1 + (1|EQID))` Can you modify it to make it break in the way that your code does? – Ben Bolker Dec 19 '22 at 14:05
  • @BenBolker My Rtot has several NA values so when I run : lmer(Rtot ~ 1 + (1|EQID))...i get an error saying: Error in KhatriRao(sm, t(mm)) : (p <- ncol(X)) == ncol(Y) is not TRUE........I want run this model to get ranef values....where I can have NA values at approriate places where I have NA values in Rtot. Please help. thanks – Rashid Shams Dec 20 '22 at 05:08
  • We can't help you unless you can give us a reproducible example. I have been unable to construct one, so you'll have to either share your data or share code that will generate such an example, see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Ben Bolker Dec 20 '22 at 12:17
  • @BenBolker I updated my question with a sample dataset and my exact needs. Please see if it makes sense now. Thanks – Rashid Shams Dec 20 '22 at 23:21
  • @MrFlick I don't want to drop NAs (they are NA values not NaNs). I want the output from ranef(event_lme) to have NA at appropriate places same as in Rtot – Rashid Shams Dec 20 '22 at 23:24
  • The sample data set you gave isn't sufficient. (1) You have a string included in `Rtot`, which means we get the error "> Error in mkRespMod(fr, REML = REMLpass) : response must be numeric". (2) If I use `as.numeric(Rtot)` as the response, I get "number of levels of each grouping factor must be < number of observations". Another example that doesn't give an error: `set.seed(101); n <- 500; dd2 <- data.frame(Rtot = ifelse(runif(n) < 0.2, NA, rnorm(n)), EQID = sample(1:20, size = n, replace = TRUE)); event_lme <- lmer(Rtot ~1 + (1 |EQID), data = dd2)`. ... – Ben Bolker Dec 21 '22 at 03:26
  • You might be able to use `na.action = na.exclude`. – Ben Bolker Dec 21 '22 at 03:26

0 Answers0