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

I have already checked the other questions with on this issue, but since the problem seems to be very specific they weren’t helpful.

I have a dataframe like this (this is just a quick example, example data from dput() is provided below):

year species abundance site county
2005 A       2         SH1  Göttingen
2006 A       0         SH1  Göttingen
2007 A       NA        SH1  Göttingen
2008 A       2         SH1  Göttingen
2009 A       NA        SH1  Göttingen
2010 A       2         SH1  Göttingen
2011 A       NA        SH1  Göttingen
2005 B       2         SH1  Göttingen
2006 B       0         SH1  Göttingen
2007 B       NA        SH1  Göttingen
2008 B       2         SH1  Göttingen
2009 B       NA        SH1  Göttingen
2010 B       2         SH1  Göttingen
2011 B       NA        SH1  Göttingen
2005 A       2         SH1  Göttingen
2006 A       0         SH1  Göttingen
2007 A       NA        SH1  Göttingen
2008 A       2         SH1  Göttingen
2009 A       NA        SH1  Göttingen
2010 A       2         SH1  Göttingen
2011 A       NA        SH1  Göttingen
2005 A       2         SH2  Göttingen
2006 A       0         SH2  Göttingen
2007 A       NA        SH2  Göttingen
2008 A       2         SH2  Göttingen
2009 A       NA        SH2  Göttingen
2010 A       2         SH2  Göttingen
2011 A       NA        SH2  Göttingen

It contains the abundance for 11 species on several different sites per county (more than 1500 sites in over 400ish counties) for each year 2005-2011. for each site, in every county, every year, all species have been accounted for, so there is either an NA, or a number in abundance for every year. The number of sites varies per county.

I would like to run the following loop to put the abundance into several columns: It should create a linear model to calculate population trends over these years and put the output in an additional row. In the end I would like to have a trend for every species on every site over the years:

alldata_lm$slope_abundance_plot <- NA
alldata_lm$p_slope_abundance_plot <- NA

species <- unique(alldata_lm$species)
sites <- unique(alldata_lm$site)

for (i in (1:length(species))) {
  for (k in(1:length(sites))) {        
    print(c(i,k))
    lm1 <-  lm(abundance ~ year, data = alldata_lm[alldata_lm$species == species[i] & alldata_lm$site == sites[k],], na.action=na.omit)
    alldata_lm$slope_abundance_plot[alldata_lm$species == species[i] & alldata_lm$site == sites[k]] <- coefficients(lm1)[2]
    if (nrow(coef(summary(lm1)))>1){ alldata_lm$p_slope_abundance_plot[alldata_lm$species == species[i] & alldata_lm$site == sites[k]] <- coef(summary(lm1))[2,4]}
  }  
}

However, when I do, it returns the following error:

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  alle Fälle NA

The same loop works perfectly with a very similar dataframe, the only difference is that the current dataframe contains far more NA’s.

Deleting the NA’s prior to running the loop does not help. I get the error message no matter if there are any NA’s in the abundance column or not. I think the error might occur somewhere else. The year column does never contain any missing values.

I’d greatly appreciate any help! Thanks

EXAMPLE DATA

 structure(list(site = structure(c(700L, 700L, 700L, 700L, 700L, 
700L, 700L), .Label = c("bb1", "bb100", "bb101", "bb104", "bb107", 
"bb108", "bb109", "bb11", "bb111", "bb113", "bb115", "bb116", 
"bb117", "bb118", "bb119", "bb120", "bb121", "bb122", "bb123", 
"bb124", "bb125", "bb126", "bb127", "bb129", "bb130", "bb131", 
"bb132", "bb134", "bb135", "bb138", "bb139", "bb14", "bb140", 
"bb143", "bb147", "bb15", "bb150", "bb152", "bb154", "bb155", 
"bb156", "bb157", "bb158", "bb159", "bb163", "bb164", "bb166", 
"bb167", "bb169", "bb170", "bb171", "bb172", "bb173", "bb174", 
"bb175", "bb176", "bb177", "bb178", "bb179", "bb180", "bb181", 
"bb183", "bb186", "bb187", "bb188", "bb19", "bb191", "bb192", 
"bb193", "bb194", "bb197", "bb198", "bb199", "bb20", "bb200", 
"bb201", "bb202", "bb203", "bb204", "bb205", "bb207", "bb208", 
"bb209", "bb21", "bb210", "bb211", "bb212", "bb213", "bb215", 
"bb216", "bb217", "bb218", "bb219", "bb220", "bb221", "bb224", 
"bb225", "bb228", "bb23", "bb230", "bb232", "bb234", "bb237", 
"bb239", "bb242", "bb27", "bb32", "bb35", "bb37", "bb38", "bb39", 
"bb4", "bb40", "bb41", "bb47", "bb49", "bb53", "bb55", "bb58", 
"bb59", "bb6", "bb60", "bb63", "bb65", "bb66", "bb7", "bb70", 
"bb72", "bb73", "bb76", "bb77", "bb79", "bb8", "bb80", "bb81", 
"bb82", "bb84", "bb85", "bb87", "bb89", "bb9", "bb90", "bb91", 
"bb92", "bb93", "bb94", "bb96", "bb97", "bb98", "be14", "be15", 
"be17", "be30", "bw10", "bw100", "bw104", "bw108", "bw111", "bw112", 
"bw12", "bw120", "bw124", "bw126", "bw13", "bw144", "bw146", 
"bw175", "bw183", "bw192", "bw193", "bw198", "bw199", "bw200", 
"bw202", "bw208", "bw210", "bw211", "bw213", "bw215", "bw219", 
"bw225", "bw226", "bw229", "bw236", "bw243", "bw257", "bw262", 
"bw266", "bw268", "bw283", "bw294", "bw3", "bw30", "bw307", "bw326", 
"bw327", "bw338", "bw339", "bw341", "bw35", "bw36", "bw360", 
"bw368", "bw380", "bw381", "bw397", "bw405", "bw42", "bw53", 
"bw58", "bw6", "bw7", "bw84", "bw89", "bw91", "bw92", "bw96", 
"bw97", "by10", "by103", "by109", "by11", "by110", "by111", "by112", 
"by113", "by114", "by115", "by116", "by117", "by118", "by120", 
"by122", "by125", "by126", "by127", "by128", "by129", "by130", 
"by134", "by137", "by14", "by142", "by144", "by146", "by147", 

Leave a Comment

tech