2

I have a dataset (data1) as below:

T,D,P,R
1,0,1,1  
2,1,1,3  
3,0,1,7  
1,0,2,2  
2,1,2,3  
3,1,2,7 
1,0,3,1
2,1,3,4
3,1,3,7
1,1,4,1
2,1,4,3 
3,0,4,7

To start with, all variables except the response (which is R) are specified as factors (so D, T and P are factors). I then fit the model:

model1 <- lme(R~D+T,random=~1|P, method="REML",data=data1)

Then I want to use lsmeans to average across factor levels of D and P.

To do this I type:

lsm_model <- lsmeans(model1,~T)

Doing this, I get two error messages:

"Error in [.data.frame(tbl, , vars, drop = FALSE) : undefined columns selected

And

Error in ref.grid(object = list(modelStruct = list(reStruct = list(Plot = -11.7209195395097)), : Perhaps a 'data' or 'params' argument is needed"

Why can't I fit lsmeans to this model? I have used lsmeans many times without any problems.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 2
    Did your model actually run? You are missing the tilde in your `random` statement. Once I get the model working I can't reproduce your errors. – aosmith Aug 01 '17 at 15:15
  • @aosmith, thank for the notification of the missing tilde. I have edited the question now. Yes, the model works fine (the missing tilde was just a typing mistake in the question). So you dont get an error message when you apply LSmeans to this model? I have tried to restart R now, but I still get the same error messages as above. – Ditlev Reventlow Aug 01 '17 at 15:36
  • I can't replicate either. Make sure your session is clean (no leftover objects)? I have nlme version 3.1-131, lsmeans 2.26-3 – Ben Bolker Aug 01 '17 at 15:48
  • Thank you @Ben Bolker, I have the same version as you mention. So this should not be an issue. In regards to the clean session, do you think I should try to do something more than just restarting R? – Ditlev Reventlow Aug 01 '17 at 16:40
  • 1
    Could you post the results of `sessionInfo()` after running your code? – Marco Sandri Aug 01 '17 at 16:49
  • make sure your environment is clean (no pre-existing objects; `rm(list=ls())` if necessary) – Ben Bolker Aug 01 '17 at 17:30
  • also, show us `str(data1)` – Ben Bolker Aug 01 '17 at 17:30
  • I tried again this morning and now it worked!! This is even though I haven't changed anything in my coding. I still need to solve the problem (finding out what causes the problem) because I have the same problem for another dataset that I also need to run. Regarding this error: "Error in `[.data.frame`(tbl, , vars, drop = FALSE) : undefined columns selected" What does this error refer to? – Ditlev Reventlow Aug 02 '17 at 11:25
  • @MarcoSandri. Here is the session info: R version 3.3.3 (2017-03-06) Platform: i386-w64-mingw32/i386 (32-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 locale: [1] LC_COLLATE=Danish_Denmark.1252 LC_CTYPE=Danish_Denmark.1252 LC_MONETARY=Danish_Denmark.1252 [4] LC_NUMERIC=C LC_TIME=Danish_Denmark.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] gof_0.9.1 nlme_3.1-131 lsmeans_2.26-3 estimability_1.2 – Ditlev Reventlow Aug 02 '17 at 11:28
  • And the rest that could not fit in the previous comment: loaded via a namespace (and not attached): [1] Rcpp_0.12.10 lattice_0.20-34 codetools_0.2-15 mvtnorm_1.0-5 zoo_1.7-14 MASS_7.3-45 [7] grid_3.3.3 plyr_1.8.4 xtable_1.8-2 coda_0.19-1 multcomp_1.4-6 Matrix_1.2-8 [13] sandwich_2.3-4 splines_3.3.3 TH.data_1.0-8 tools_3.3.3 survival_2.40-1 – Ditlev Reventlow Aug 02 '17 at 11:29
  • 2
    I have solved the problem now. It was a matter of that one variable was not perceived to be part of the dataset, even though I had done a similar approach several times where it worked fine. Specifying that the variable was inside the dataset did the job. – Ditlev Reventlow Aug 02 '17 at 12:43
  • How did you solve the problem? Could you write a short answer, please? I've run into the same issue. – Robert Jan 05 '18 at 18:36
  • I created the variable T from another variable (that I here call Y). I initially did this by typing T<-factor(Dataset$Y) And I thought that this was ok. However, R did not perceive it to be part of the dataset before I typed Dataset$T<-factor(Dataset$Y) – Ditlev Reventlow Jan 07 '18 at 10:26
  • 1
    I have the same error but my issue was that I was putting the model formula in quotes, ie, lm("a~b", data=df). – gcamargo Mar 14 '18 at 19:14

0 Answers0