-1

I'm new to R and don't understand much about how it works. I'm trying to run this code however the "Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases" constantly appears and my data actually has no NA. So what should I do?

> library("moult")
> 
> mym = read.csv("Test.csv", sep=";", head=T)
> head(mym)
    Age Sex  MoultPresence      Score Prim Habitat Jul.Day
1 Adult   1             1 5554210000   22   Beach     123
2 Adult   2             1 5555321000   26 Estuary     124
3 Adult   1             1 5555553200   35 Estuary     124
4 Adult   1             1 4440000000   12   Beach     126
5 Adult   2             1 5555532100   31 Mudflat     127
6 Adult   1             1 5555321000   26 Mudflat     127

> if (is.numeric(mym$Prim)) {scores <- format(mym$Prim, scientific = FALSE, trim = TRUE)} else {scores <- mym$Prim}
> mscores <- substr(scores, 1, 9)
> mym$Date <- date2days(mym$Date, dateformat = "dd-mm-yyyy", startmonth = 9)
> m88.2 <- moult(Prim ~ Jul.Day, data = mym)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

> summary(m88.2)
Error in summary(m88.2) : object 'm88.2' not found

> m88c <- moult(Prim ~ Jul.Day, data = mym, type = 3)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

> summary(m88c)
Error in summary(m88c) : object 'm88c' not found

> durationmean2ab <- function(duration, mean) {
+   ab <- c(-mean/duration, 1/duration) 
+   names(ab) <- c("intercept", "slope") 
+   return(ab) 
+ }
> uz1 <- durationmean2ab(coef(m88c, "duration"), coef(m88c, "mean"))
Error in coef(m88c, "mean") : object 'm88c' not found

> 
> ssex <- ifelse(mym$Sex == 1 | mym$Sex == 3, "male", ifelse(mym$Sex == 2 | mym$Sex == 4, "female", NA))
> mym$ssex <- as.factor(ssex)
> mmf <- moult(Prim ~ Jul.Day | ssex | ssex, data = mym, type = 3)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

Could any of you have any idea about how to fix it?

  • Welcome to Stack Overflow! This is probably going to be a bit difficult, as the `moult` package is fairly specialized. Someone will have to work through the guts of `moult` to find out what's going wrong. Can you please include data and/or code that will provide us with a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) ? Alternatively, you *might* have more luck on the `r-sig-ecology@r-project.org` mailing list. – Ben Bolker Apr 10 '16 at 21:25

1 Answers1

1

It helps to Read the Fine Manual ... Taking a quick look at the vignette for the moult package (emphasis added):

formula can have five parts, of which the first two (moult.index and days) must always be provided:

moult.index ~ days | x1 + x2 | y1 + y2 | z1

On the left-hand-side of the ∼ the vector of moult indices (between 0 and 1, one for each individual) is given, the first part after the ∼ requires a vector of corresponding days on which these individuals were observed (number of days since, e.g., 1 July).

Earlier in the same document:

This moult index can be the original sum of moult scores, or some transformation of these to make the index linear... It is convenient to scale this index to range from 0 to 1.

So I'd suggest starting with:

mym <- transform(mym, Prim_scaled = (Prim-min(Prim))/(max(Prim)-min(Prim)))
moult(Prim_scaled ~ Jul.Day, data = mym)

This appears to work OK (with moult version 1.4) with the subset of the data you've shown us:

mym <- read.table(header=TRUE,
                  text="
Age Sex  MoultPresence      Score Prim Habitat Jul.Day
1 Adult   1             1 5554210000   22   Beach     123
2 Adult   2             1 5555321000   26 Estuary     124
3 Adult   1             1 5555553200   35 Estuary     124
4 Adult   1             1 4440000000   12   Beach     126
5 Adult   2             1 5555532100   31 Mudflat     127
6 Adult   1             1 5555321000   26 Mudflat     127")

mym <- transform(mym, Prim_scaled = (Prim-min(Prim))/(max(Prim)-min(Prim)))
moult(Prim_scaled ~ Jul.Day, data = mym)

Unless you can provide a reproducible example, we probably can't get any farther.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Hi Ben, thanks for the suggestion. Tried to transform my data but I still get the same error. – Bianca Vieira Apr 11 '16 at 10:49
  • Tried to include a feather mass data and it helped but now I have another error `> feather.mass <- c(20.4, 20.8, 21.5, 22.8, 24.4, 25.6, 26.3, 25.7, 25.7) > mym$Prim <- ms2pfmg(mscores, feather.mass) > mym$Date <- date2days(mym$Date, dateformat = "dd-mm-yyyy", startmonth = 9) > m88.2 <- moult(Prim ~ Jul.Day, data = mym) Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, : length of 'dimnames' [2] not equal to array extent` – Bianca Vieira Apr 11 '16 at 10:58