0
votes

I am running a Linear Mixed Effect Model in R and I was able to successfully run my code and get results.

My code is as follow:

library(lme4)
library(multcomp)
read.csv(file="bh_new_all_woas.csv")  
whb=read.csv(file="bh_new_all_woas.csv")  
attach(whb)  
head(whb)  
whb.model = lmer(Density ~ distance + (1|Houses) + Cats, data = whb)  
summary(whb.model)

However, I would like to do a comparison of my distance fixed factor that has 4 levels to it. I tried running a lsmean as followed:

lsmeans(whb.model, pairwise ~ distance, adjust = "tukey") 

This error popped up:

Error in match(x, table, nomatch = 0L) : 'match' requires vector arguments

I also tried glht using this code:

glht(whb.model, linfct=mcp(distance="tukey"))

and got the same results. A sample of my data is as follows:

Houses  distance   abund       density      
House 1 20         0           0
House 1 120        6.052357    0.00077061
House 1 220        3.026179    0.000385305
House 1 320        7.565446    0.000963263
House 2 20         0           0
House 2 120        4.539268    0.000577958
House 2 220        6.539268    0.000832606
House 2 320        5.026179    0.000639953
House 3 20         0           0
House 3 120        6.034696    0.000768362
House 3 220        8.565446    0.001090587
House 3 320        5.539268    0.000705282
House 4 20         0           0
House 4 120        6.052357    0.00077061
House 4 220        8.052357    0.001025258
House 4 320        2.521606    0.000321061
House 5 20         4.513089    0.000574624
House 5 120        6.634916    0.000844784
House 5 220        4.026179    0.000512629
House 5 320        5.121827    0.000652131
House 6 20         2.513089    0.000319976
House 6 120        9.308185    0.001185155
House 6 220        7.803613    0.000993587
House 6 320        6.130344    0.00078054
House 7 20         3.026179    0.000385305
House 7 120        9.052357    0.001152582
House 7 220        7.052357    0.000897934
House 7 320        6.547785    0.00083369
House 8 20         5.768917    0.000734521
House 8 120        4.026179    0.000512629
House 8 220        4.282007    0.000545202
House 8 320        7.537835    0.000959747
House 9 20         3.513089    0.0004473
House 9 120        5.026179    0.000639953
House 9 220        8.052357    0.001025258
House 9 320        9.573963    0.001218995
House 10 20        2.255828    0.000287221
House 10 120       5.255828    0.000669193
House 10 220       10.060874   0.001280991
House 10 320       8.539268    0.001087254

Does anyone have any suggestions on how to fix this problem?

1
is distance a factor ? if not try as.factor(distance) in lmer model.D_Williams
I'm voting to close this question as off-topic because it is about how to use R without a reproducible example.gung - Reinstate Monica
Made a few corrections hope that clears things upEmkraw

1 Answers

3
votes

So which problem is it that needs fixing? One issue is the model, and another is the follow-up to it.

The model displayed is fitted using the fixed effects ~ distance + Cats. Now, Cats is not in the dataset provided, so that's an issue. But aside from that, distance enters the model as a quantitative predictor (if I am to believe the read.csv statements etc.). This model implies that changes in the expected Density are proportional to changes in distance. Is that a reasonable model? Maybe, maybe not. But is it reasonable to follow that up with multiple comparisons for distance? Definitely not. From this model, the change between distances of 20 to 120 will be exactly the same as the change between distances of 120 and 220. The estimated slope of distance, from the model summary, embodies everything you need to know about the effect of distance. Multiple comparisons should not be done.

Now, one might guess from the question that what you really had wanted to do was to fit a model where each of the four distances has its own effect, separate from the other distances. That would require a model with factor(distance) as a predictor; in that case, factor(distance) will account for 3 degrees of freedom rather than 1 d.f. for distance as a quantitative predictor. For such a model, it is appropriate to follow it up with multiple comparisons (unless possibly distance also interacts with some other predictors). If you were to fit such a model, I believe you will find there will be no errors in your lsmeans call (though you need a library("lsmeans") statement, not shown in your code.

Ultimately, getting programs to run without error is not necessarily the same as producing sensible or meaningful answers. So my real answer is to consider carefully what is a reasonable model for the data. I might suggest seeking one-on-one help from a statistical consultant to make sure you understand the modeling issues. Once that is settled, then appropriate interpretation of that model is the next step; and again, that may require some advice.

Additional minor notes about the code provided:

  • The first read.csv call accomplishes nothing because it doesn't store the data.
  • R is case-sensitive, so technically, Density isn't in your dataset either
  • When the data frame is attached, you don't also need the data argument in the lmer call.
  • The apparent fact that Houses has levels "House 1", "House 2", etc. is messed-up in your listing because the comma delimiters in your data file are not shown.