Exclude NA values only and not entire rows in a lm in R?

Viewed 126

If I have a dataset that looks like the following, looking at species richness of spiders in different habitats of a garden.

  'data.frame': 6 obs. of  5 variables:
 $ ID           : int  1 2 3 4 5 6
 $ species_count: num  10 13 15 17 22 9
 $ habitat_type : Factor w/ 2 levels "wall","tree": 1 2 1 2 1 2
 $ wall_height  : num  153 NA 160 NA 170 NA
 $ tree_diameter: num  NA 48 NA 52 NA 71

I want to create a lm with species_count as the dependent variable and habitat_type, wall_height and tree_diameter as the independent variables, however the NA's are tricky.

lm.1 <- lm(species_count ~ habitat_type + wall_height + tree_diameter,
           data = DF, na.action = na.exclude)

throws up the following error:

Error in contrasts<-(tmp, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels

as na.exclude and na.omit delete the entire rows.

Using:

DF$wall_height <- na.exclude(DF$wall_height)

and

DF$tree_diameter <- na.exclude(DF$tree_diameter)

just repeats the values, giving tree_diameter values to wall and vice versa, like so:

DF[1,]
  ID species_count habitat_type wall_height tree_diameter
1  1            10         wall         153            48

Is there a way to omit NA values only whilst retaining the rest of the information within the row, or will I have to use separate linear models?

Thanks in advance for any help and hope that I've been clear enough in explaining the issue.

1 Answers

The fundamental problem is that

wall_height doesn't apply to the tree obs and vice versa.

So there is nothing to be gained by trying to analyze the data from wall and tree habitats together. In principle, you can compare the two habitats, and then evaluate how habitat-specific characteristics are associated with species numbers within a habitat.

In practice, you face a problem of very few observations. Usually you want about 10 cases per predictor that you are using in your model. You might be able to do an adequate comparison of the 2 habitats, but any results within a habitat, with only 3 observations each, will be highly suspect.

A couple of other thoughts. First, count data are often better analyzed with a different type of model, a Poisson generalized linear model. Second, the numbers of species are presumably represented by different numbers of individuals of each. There is probably some information to be gleaned from that, which should be explained in the ecology literature on species diversity.

Related