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

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

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):

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:

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

回答1:

If I print your subset, I see this:

    site year                species abundance                  county slope_abundance_plot p_slope_abundance_plot 61  sh47 2005 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA 75  sh47 2006 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA 76  sh47 2007 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA 91  sh47 2008 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA 92  sh47 2009 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA 93  sh47 2010 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA 134 sh47 2011 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA 

As you see, all abundance values are NA, which is what the error message was telling you. You should use tryCatch to handle these subsets.

(Btw, the dput output is so large because it includes all factor levels.)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!