简体   繁体   中英

Two-Way Repeated Measures ANOVA: Error in lm.fit()... 0 non-na cases (rstatix)

I am trying to run a Two-Way Repeated Measures ANOVA using the R Statix package based on the description from here however I am running into the subject error with my dataset. The data consist of 10 replicate biomass measurements recorded at each of 8 Locations across 5 months (10 reps x 8 locations x 5 months = 400 observations).

head(biomass)

数据集

However, when I run this:

library(rstatix)

res.aov <- anova_test(
  data = biomass, dv = Biomass, wid = ID,
  within = c(Location, SampleMonth)
)

I get the error:

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

I've looked at similar posts here and here that seem to indicate that the error is caused by NA in the lm.

Based on the first post above, I tried to run:

lm(Biomass~Location:SampleMonth, data =biomass)

and I noticed that one Location:SampleMonth value shows an NA coefficient (LocationMPE:SampleMonth10 ). However, if I remove all the data from this Location and try again, all that happens is that another Location:SampleMonth coefficient becomes NA. This seems to occur with whatever combination (even just 2) locations.

Am I doing something wrong?

Below is a dput() of my entire dataset:

biomass <- structure(list(ID = structure(1:400, .Label = c("1", "2", "3", 
    "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
    "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", 
    "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", 
    "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", 
    "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", 
    "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", 
    "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", 
    "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", 
    "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", 
    "103", "104", "105", "106", "107", "108", "109", "110", "111", 
    "112", "113", "114", "115", "116", "117", "118", "119", "120", 
    "121", "122", "123", "124", "125", "126", "127", "128", "129", 
    "130", "131", "132", "133", "134", "135", "136", "137", "138", 
    "139", "140", "141", "142", "143", "144", "145", "146", "147", 
    "148", "149", "150", "151", "152", "153", "154", "155", "156", 
    "157", "158", "159", "160", "161", "162", "163", "164", "165", 
    "166", "167", "168", "169", "170", "171", "172", "173", "174", 
    "175", "176", "177", "178", "179", "180", "181", "182", "183", 
    "184", "185", "186", "187", "188", "189", "190", "191", "192", 
    "193", "194", "195", "196", "197", "198", "199", "200", "201", 
    "202", "203", "204", "205", "206", "207", "208", "209", "210", 
    "211", "212", "213", "214", "215", "216", "217", "218", "219", 
    "220", "221", "222", "223", "224", "225", "226", "227", "228", 
    "229", "230", "231", "232", "233", "234", "235", "236", "237", 
    "238", "239", "240", "241", "242", "243", "244", "245", "246", 
    "247", "248", "249", "250", "251", "252", "253", "254", "255", 
    "256", "257", "258", "259", "260", "261", "262", "263", "264", 
    "265", "266", "267", "268", "269", "270", "271", "272", "273", 
    "274", "275", "276", "277", "278", "279", "280", "281", "282", 
    "283", "284", "285", "286", "287", "288", "289", "290", "291", 
    "292", "293", "294", "295", "296", "297", "298", "299", "300", 
    "301", "302", "303", "304", "305", "306", "307", "308", "309", 
    "310", "311", "312", "313", "314", "315", "316", "317", "318", 
    "319", "320", "321", "322", "323", "324", "325", "326", "327", 
    "328", "329", "330", "331", "332", "333", "334", "335", "336", 
    "337", "338", "339", "340", "341", "342", "343", "344", "345", 
    "346", "347", "348", "349", "350", "351", "352", "353", "354", 
    "355", "356", "357", "358", "359", "360", "361", "362", "363", 
    "364", "365", "366", "367", "368", "369", "370", "371", "372", 
    "373", "374", "375", "376", "377", "378", "379", "380", "381", 
    "382", "383", "384", "385", "386", "387", "388", "389", "390", 
    "391", "392", "393", "394", "395", "396", "397", "398", "399", 
    "400"), class = "factor"), Location = structure(c(6L, 7L, 5L, 
    8L, 6L, 6L, 8L, 5L, 3L, 8L, 7L, 7L, 8L, 6L, 8L, 1L, 8L, 4L, 8L, 
    8L, 1L, 3L, 6L, 3L, 7L, 3L, 2L, 8L, 6L, 6L, 4L, 3L, 4L, 3L, 6L, 
    2L, 2L, 3L, 3L, 7L, 2L, 1L,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L, 
    2L, 7L, 7L, 6L, 5L, 2L, 7L, 4L, 7L, 4L, 5L, 5L, 5L, 6L, 5L, 3L, 
    1L, 8L, 4L, 1L, 5L, 2L, 4L, 6L, 3L, 3L, 4L, 2L, 5L, 7L, 8L, 5L, 
    6L, 1L, 8L, 2L, 1L, 7L, 1L, 5L, 4L, 1L, 6L, 6L, 3L, 3L, 4L, 3L, 
    2L, 8L, 5L, 2L, 3L, 6L, 6L, 5L, 2L, 5L, 7L, 6L, 6L, 3L, 8L, 6L, 
    7L, 3L, 4L, 2L, 2L, 6L, 5L, 4L, 3L, 3L, 1L, 6L, 1L, 4L, 6L, 2L, 
    6L, 4L, 6L, 2L, 7L, 8L, 5L, 4L, 3L, 5L, 1L, 2L, 3L, 6L, 2L, 3L, 
    8L, 7L, 6L, 3L, 8L, 6L, 3L, 8L, 6L, 6L, 3L, 4L, 6L), .Label = c("BR", 
    "HBY", "MBB", "MCB", "MEB", "MEL", "MPD", "MPE"), class = "factor"), 
        SampleMonth = structure(c(1L, 1L, 5L, 3L, 2L, 1L, 1L, 4L, 
        2L, 4L, 4L, 2L, 4L, 1L, 5L, 3L, 2L, 1L, 3L, 2L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 3L, 5L, 3L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 
        1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 4L, 3L, 1L, 5L, 3L, 4L, 5L, 
        1L, 5L, 5L, 1L, 5L, 5L, 5L, 3L, 1L, 1L, 1L, 1L, 1L, 5L, 4L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 1L, 1L, 1L, 5L, 4L, 
        3L, 1L, 5L, 2L, 5L, 2L, 1L, 3L, 1L, 1L, 1L, 3L, 2L, 5L, 1L, 
        1L, 2L, 1L, 3L, 1L, 1L, 4L, 4L, 5L, 1L, 5L, 3L, 5L, 1L, 1L, 
        3L, 4L, 2L, 1L, 4L, 2L, 4L, 1L, 1L, 2L, 5L, 4L, 1L, 2L, 2L, 
        1L, 3L, 1L, 1L, 5L, 2L, 4L, 5L, 1L, 1L, 5L, 3L, 2L, 1L, 2L, 
        4L, 3L, 5L, 1L, 1L, 3L, 5L, 2L, 5L, 3L, 4L, 1L, 2L, 4L, 4L, 
        5L, 5L, 5L, 5L, 3L, 1L, 4L, 2L, 2L, 3L, 2L, 1L, 4L, 2L, 5L, 
        4L, 4L, 2L, 2L, 5L, 1L, 2L, 2L, 2L, 4L, 4L, 2L, 2L, 3L, 5L, 
        4L, 3L, 2L, 3L, 4L, 2L, 4L, 5L, 5L, 5L, 1L, 3L, 3L, 3L, 3L, 
        4L, 3L, 2L, 1L, 2L, 3L, 3L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 3L, 
        2L, 4L, 4L, 5L, 3L, 2L, 1L, 3L, 4L, 5L, 5L, 1L, 1L, 4L, 2L, 
        5L, 4L, 2L, 3L, 1L, 2L, 3L, 3L, 2L, 4L, 3L, 3L, 5L, 2L, 4L, 
        3L, 3L, 5L, 4L, 4L, 2L, 2L, 2L, 5L, 5L, 5L, 2L, 2L, 3L, 2L, 
        4L, 4L, 5L, 4L, 5L, 5L, 4L, 4L, 3L, 4L, 5L, 3L, 2L, 5L, 4L, 
        5L, 5L, 3L, 3L, 5L, 3L, 4L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 2L, 
        5L, 5L, 2L, 3L, 4L, 5L, 2L, 4L, 5L, 2L, 2L, 3L, 3L, 4L, 4L, 
        2L, 3L, 2L, 3L, 3L, 3L, 5L, 4L, 3L, 3L, 4L, 4L, 3L, 2L, 2L, 
        3L, 4L, 2L, 5L, 2L, 2L, 3L, 5L, 2L, 2L, 3L, 2L, 4L, 3L, 5L, 
        2L, 2L, 4L, 3L, 5L, 5L, 4L, 3L, 3L, 2L, 4L, 3L, 5L, 4L, 3L, 
        2L, 2L, 3L, 2L, 2L, 3L, 4L, 5L, 5L, 5L, 5L, 2L, 5L, 5L, 4L, 
        4L, 4L, 5L, 4L, 4L, 4L, 3L, 5L, 3L, 5L, 4L, 5L, 4L, 5L, 4L, 
        3L, 4L, 4L, 3L, 3L, 3L, 5L, 5L, 3L, 5L, 2L, 4L, 3L, 4L, 4L, 
        3L, 4L), .Label = c("06", "07", "08", "09", "10"), class = "factor"), 
        Biomass = c(0.1, 0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 9.99, 19.99, 36.99, 39.99, 50, 50, 51, 60, 
        60, 69.99, 80, 81, 84, 86.99, 89.99, 100, 100, 100, 100.99, 
        117, 119.99, 119.99, 119.99, 125, 129.99, 130, 139.99, 140, 
        150, 150, 152.99, 159.99, 169.99, 170, 179.99, 184.99, 189.99, 
        189.99, 192.99, 199.99, 199.99, 210, 219.99, 219.99, 230, 
        230, 235, 248, 250, 269.99, 269.99, 269.99, 276, 279.99, 
        280, 282, 289.99, 289.99, 300, 300, 300, 304, 310, 310, 319.99, 
        319.99, 326, 329.99, 330, 335.99, 339.99, 339.99, 340, 350, 
        350, 360, 362, 369.99, 369.99, 369.99, 375, 379.99, 380, 
        383, 384, 390, 390, 399.99, 400, 419.99, 420, 423.99, 429.99, 
        430, 430, 430, 435.99, 440, 450, 459.99, 459.99, 465, 469.99, 
        479.99, 480, 480, 482, 484, 490, 490, 499.99, 500, 509.99, 
        519.99, 529.99, 530, 544.99, 556.99, 559.99, 560, 569.99, 
        569.99, 573, 579, 590, 590, 597, 609.99, 609.99, 609.99, 
        610, 610, 630, 630, 640, 650, 659.99, 659.99, 659.99, 667, 
        670, 680, 680, 680, 690, 700, 702, 709.99, 709.99, 709.99, 
        719.99, 719.99, 729.99, 730, 739.99, 740, 744, 745.99, 749.99, 
        750, 752, 756.99, 758, 759.99, 759.99, 760, 769.99, 769.99, 
        769.99, 780, 789.99, 789.99, 789.99, 790, 794, 809.99, 810, 
        810, 810, 819.99, 829.99, 829.99, 830, 840, 840, 850, 869, 
        870, 870, 880, 880, 890, 890, 890, 898.99, 900, 900, 909.99, 
        910, 919.99, 920, 930, 939.99, 940, 949.99, 950, 950, 959.99, 
        960, 970, 979.99, 979.99, 980, 980.99, 989.99, 990, 997.99, 
        1000, 1019.99, 1020, 1020, 1029.99, 1030, 1030, 1040, 1049.99, 
        1050, 1050, 1059.99, 1069.99, 1080, 1100, 1100, 1120, 1130, 
        1130.99, 1139.99, 1153, 1158, 1170, 1170, 1177, 1180, 1180, 
        1180, 1180, 1189.99, 1190, 1200, 1219.99, 1220, 1240, 1259.99, 
        1259.99, 1279, 1279.99, 1280.99, 1300, 1309.99, 1310, 1316, 
        1320, 1330, 1339.99, 1339.99, 1340, 1345, 1359.99, 1360, 
        1369, 1370, 1379.99, 1400, 1410, 1420, 1429.99, 1430.99, 
        1440, 1440, 1449, 1450, 1459.99, 1468.99, 1470, 1474, 1479.99, 
        1490, 1490, 1490, 1499.99, 1510, 1520, 1529.99, 1540, 1560, 
        1560, 1570, 1570, 1579.99, 1580, 1580, 1590, 1590, 1594, 
        1620, 1630, 1630, 1639.99, 1650, 1650, 1680, 1715, 1719.99, 
        1720, 1729.99, 1733, 1770, 1779.99, 1808, 1819.99, 1837, 
        1840, 1860, 1900, 1909.99, 1910, 1929.99, 1940, 1954, 2020, 
        2040, 2050, 2069.99, 2090, 2100, 2102.99, 2159.99, 2179.99, 
        2249.99, 2279.99, 2289.99, 2290, 2334, 2366, 2380, 2399.99, 
        2400, 2420, 2450, 2500, 2520, 2550, 2589.99, 2589.99, 2599.99, 
        2600, 2620, 2660, 2713, 2739.99, 2830, 2920, 2959.99, 3010, 
        3029.99, 3129.99, 3150, 3190, 3250, 3310, 3440, 3490, 3650, 
        3740, 3809.99, 3850, 3920, 5799.99, 5898.72, 6680, 6970)), class = c("spec_tbl_df", 
    "tbl_df", "tbl", "data.frame"), row.names = c(NA, -400L))

First of all, for the lm, you get NAs because you did not specify the main term:

fit = lm(Biomass ~ SampleMonth*Location,data=biomass)
table(is.na(coefficients(fit)))

FALSE 
   40

When you do a two way repeated anova, you have an experiment or study that is a repeated measure, something like many patients undergoing a treatment over a period of time etc. In your case, if there is no "repeated measure", or common individuals that underwent the same treatment, then you can use the conventional anova.

If you are interested in effects in time period and sites:

anova_test(Biomass ~ SampleMonth+Location,data=biomass)
Coefficient covariances computed by hccm()
ANOVA Table (type II tests)

       Effect DFn DFd      F        p p<.05   ges
1 SampleMonth   4 388 18.712 4.45e-14     * 0.162
2    Location   7 388  2.969 5.00e-03     * 0.051

The above assumes a common mean (or effect) for each month or each location. You can have a more complicated model which test whether there are variations in the Month effect across locations:

anova_test(Biomass ~ SampleMonth*Location,data=biomass)
Coefficient covariances computed by hccm()
ANOVA Table (type II tests)

                Effect DFn DFd      F        p p<.05   ges
1          SampleMonth   4 360 19.507 1.53e-14     * 0.178
2             Location   7 360  3.095 4.00e-03     * 0.057
3 SampleMonth:Location  28 360  1.588 3.20e-02     * 0.110

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM