Error in lm.fit 0 (non-NA) cases

Hi RStudio Community. I am wrestling with a problem that was previously raised here but never seemed to actually get answered. It's also been posted on several other sites but none have been able to get to the root of my issue.

I am trying to run a repeated measures anova and I keep getting this error about NA's despite not having any NAs in my sample. Have tried to debug to no avail. Thank you in advance.

# Here is a segment of my dataset to recreate the example:
 structure(list(record_id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 
6L, 6L, 7L, 7L, 7L, 7L), .Label = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9", "11", "13", "14", "15", "16", "17", "19", 
"20", "21", "22", "23", "24", "25", "26", "27", "29", "30", "31", 
"32", "34", "36", "37", "38", "39", "41", "42", "43", "44", "45", 
"46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", 
"57", "59", "60", "62", "63", "64", "65", "66", "69", "70", "71", 
"72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", 
"83", "84", "85", "86", "87", "88", "89", "90", "91", "93", "94", 
"96", "97", "98", "99", "100", "101", "103", "104", "105", "106", 
"107", "108", "109", "110", "111", "112", "113", "114", "115", 
"116", "117", "118", "121", "122", "123", "124", "125", "126", 
"127", "128", "129", "130", "131", "132", "133", "134", "136", 
"137", "138", "139", "141", "142", "143", "144", "145", "146", 
"147", "148", "149", "150", "151", "152", "153", "154", "156", 
"158", "159", "161", "162", "163", "164", "165", "166", "167", 
"168", "169", "171", "172"), class = "factor"), redcap_event_name = structure(c(2L, 
3L, 4L, 5L, 2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 2L, 
3L, 4L, 5L, 2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L), .Label = c("", "baseline_visit_arm_1", 
"day_1_arm_1", "day_2_arm_1", "day_3_arm_1"), class = "factor"), 
    feelings_sum_indiv = c(105L, 94L, 90L, 70L, 69L, 63L, 89L, 
    69L, 72L, 80L, 52L, 41L, 40L, 39L, 36L, 35L, 43L, 42L, 40L, 
    54L, 67L, 60L, 59L, 65L, 42L, 50L, 40L, 40L), feelings_sqrt = c(10.2469507659596, 
    9.69535971483266, 9.48683298050514, 8.36660026534076, 8.30662386291807, 
    7.93725393319377, 9.4339811320566, 8.30662386291807, 8.48528137423857, 
    8.94427190999916, 7.21110255092798, 6.40312423743285, 6.32455532033676, 
    6.2449979983984, 6, 5.91607978309962, 6.557438524302, 6.48074069840786, 
    6.32455532033676, 7.34846922834953, 8.18535277187245, 7.74596669241483, 
    7.68114574786861, 8.06225774829855, 6.48074069840786, 7.07106781186548, 
    6.32455532033676, 6.32455532033676), feelings_recip = c(0.00952380952380952, 
    0.0106382978723404, 0.0111111111111111, 0.0142857142857143, 
    0.0144927536231884, 0.0158730158730159, 0.0112359550561798, 
    0.0144927536231884, 0.0138888888888889, 0.0125, 0.0192307692307692, 
    0.024390243902439, 0.025, 0.0256410256410256, 0.0277777777777778, 
    0.0285714285714286, 0.0232558139534884, 0.0238095238095238, 
    0.025, 0.0185185185185185, 0.0149253731343284, 0.0166666666666667, 
    0.0169491525423729, 0.0153846153846154, 0.0238095238095238, 
    0.02, 0.025, 0.025)), row.names = c(NA, 28L), class = "data.frame")
tmp %>% anova_test(dv = feelings_sum_indiv, wid = record_id, within = redcap_event_name)

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
0 (non-NA) cases

Once I made the minimal necessary adjustments to make your code runnable
a) adding libraries you require (I determined these to be dplyr and rstatix)
b) assigning your example data to the name tmp

Your code as written runs and gives the output:

ANOVA Table (type III tests)

$ANOVA
             Effect DFn DFd     F     p p<.05   ges
1 redcap_event_name   3  18 1.091 0.378       0.033

$`Mauchly's Test for Sphericity`
             Effect     W    p p<.05
1 redcap_event_name 0.277 0.31      

$`Sphericity Corrections`
             Effect   GGe      DF[GG] p[GG] p[GG]<.05   HFe      DF[HF]
1 redcap_event_name 0.616 1.85, 11.08 0.364           0.877 2.63, 15.79
  p[HF] p[HF]<.05
1 0.375  
1 Like

Hi, thanks for answering.

That is really strange. I already have tidyverse and rstatix loaded. I loaded dplyr and I am still getting the same error.

I am still getting the same error.

On what data?
your own full set?, or the sample you sent to us ?

UPDATE - I restarted my RSession. Loaded rstatix, dplyr, and tidyverse (in that order). The code now works on both the reprex and my full dataset. I am guessing this means the error had something to do with packages masking some functions? Anyway, this is now resolved and I appreciate your help.

1 Like

Hi @nirgrahamuk I think I sent the all clear prematurely.

if I just add one more step where I transform the variable, then retry the anova. I am back to square one where I get the same error message. Can you see if you have the same issue?

# step one: create a new df with the data above and assign it to `tmp`
tmp$feelings_t <- (tmp$feelings_sum_indiv)^1/2
tmp %>% anova_test(dv = feelings_sum_indiv, wid = record_id, within = redcap_event_name)

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
0 (non-NA) cases

There is something about the transformation that R doesn't like and I think this is the crux of my original issue as well.

No, something else must be happening. the addition of feelings_t changes nothing for me. its unproblematic

are you running other codes? loading other libraries ?

I am not running other codes. While I am not manually loading other libraries I know that everytime I start R, it automatically loads things. Every session I start a session I get the following on my console (see below). Do think this might be related to the problem, and if so, how can I prevent them from loading upon startup each time?

Loading required package: e1071
Loading required package: survival
Loading required package: gmodels
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘gmodels’
Loading required package: nlme
Loading required package: ROCR
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘ROCR’
Loading required package: dunn.test
Loading required package: ggplot2