1

my data is nested on a group level. There are five different treatments. Within every treatment, four participants are grouped. It is about donation behavior of those participants (dependent variable = Donation, metric, in €) under competition ( Explanatory variable = Treatment, ordinal). The data structure is like this:

Treatment   Session   player.cumulative_donation:
CG             uk4rlbdo         2.5
CG             uk4rlbdo         1.4 
CG             uk4rlbdo         0
CG             uk4rlbdo         1
CG             dg0bqvit         0
CG             dg0bqvit         0
CG             dg0bqvit         0.5
CG             dg0bqvit         0
TG1            g6n3z46r         1
TG1            g6n3z46r         0
TG1            g6n3z46r         0
TG1            g6n3z46r         0.2

After computing the ANOVA based on Rcompanion, I want to perform a Posthoc test using the multcomp function.

however, if I run

library(multcomp)

posthoc = glht(model,
               linfct = mcp(Treatment="Tukey"))

I get this error message which I don't understand

Error in model.frame.lme(object) : object does not contain any data
Error in factor_contrasts(model) : 
  no ‘model.matrix’ method for ‘model’ found!

There is data stored in model:

> model
Linear mixed-effects model fit by REML
  Data: NULL 
  Log-restricted-likelihood: -166.8703
  Fixed: Donation ~ Treatment 
 (Intercept) TreatmentTG1 TreatmentTG2 TreatmentTG3 TreatmentTG4 
   0.7492227    1.3343727    0.2981268    1.4943010    0.5274175 

Random effects:
 Formula: ~1 | Session
        (Intercept) Residual
StdDev:   0.1759392 1.651152

Number of Observations: 88
Number of Groups: 27

The variables are:

$ player.cumulative_donation: num  2.5 1.4 0 1 0 0 0.5 0 1 0 ...
$ player.treatmentgroup     : chr  "CG" "CG" "CG" "CG" ...
$ Session code              : chr  "uk4rlbdo" "uk4rlbdo" "uk4rlbdo" "uk4rlbdo" ...

EDIT: The R command to create model:

library(nlme)

model = lme(Donation ~ Treatment, random=~1|Session,
            method="REML")

anova.lme(model,
          type="sequential",
          adjustSigma = FALSE)

Output of dput(head(SPSS_Data.df,10)):

structure(list(Participant_id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 
10), participant.id_in_session = c(1, 2, 3, 4, 1, 2, 3, 1, 2, 
3), participant.code = c("hcj5o43a", "ugiv2jlq", "53vepzb7", 
"j2k7noqy", "njm1sr5d", "c2phh8p1", "5xaot5ii", "lvfkfw72", "05pjmgwp", 
"o0yt5qbt"), `Session code` = c("uk4rlbdo", "uk4rlbdo", "uk4rlbdo", 
"uk4rlbdo", "dg0bqvit", "dg0bqvit", "dg0bqvit", "8stn6uxo", "8stn6uxo", 
"8stn6uxo"), player.cumulative_donation = c(2.5, 1.4, 0, 1, 0, 
0, 0.5, 0, 1, 0), player.treatmentgroup = c("CG", "CG", "CG", 
"CG", "CG", "CG", "CG", "CG", "CG", "CG"), TG_coded = c(0, 0, 
0, 0, 0, 0, 0, 0, 0, 0), CG_Dummy = c(1, 1, 1, 1, 1, 1, 1, 1, 
1, 1), TG1_Dummy = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TG2_Dummy = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), TG3_Dummy = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), TG4_Dummy = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Gruppe = c(1, 
1, 1, 1, 2, 2, 2, 3, 3, 3), `Perceived Competition` = c(2, 1, 
1, 1, 3, 1, 2, 2, 1, 1), `Influenced behavior` = c(2, 2, 1, 1, 
3, 1, 4, 1, 1, 1), `Donate more` = c(3, 3, 1, 1, 1, 3, 2, 1, 
1, 1), `Donate less` = c(3, 3, 1, 1, 3, 3, 3, 1, 1, 1)), row.names = c(NA, 
10L), class = "data.frame")
R_Beginner
  • 15
  • 5
  • Hi R_Beginner. Welcome to StackOverflow! Please read the info about [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and how to give a [minimale reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! – dario Feb 24 '20 at 13:57
  • 2
    Show your R command to create `model`. – Edward Feb 24 '20 at 13:59
  • And also, the data you have showed is definitely not what you have tested in the model.. the coefficients are different – StupidWolf Feb 24 '20 at 14:06
  • Thank you for the input of any kind, I edited my post and included the real data. – R_Beginner Feb 24 '20 at 14:18

1 Answers1

1

The regression works if the data is a variable in the environment, but for downstream analysis, they required it to be stored as a data.frame inside the lme object:

For example, this works perfectly

library(nlme)
library(multcomp)

SPSS_Data.df = data.frame(
"player.treatmentgroup"=sample(c("TG1","TG2","TG3"),100,replace=TRUE),
"player.cumulative_donation"=rnorm(100),
"Session code" = sample(c("uk4rlbdo","dg0bqvit"),100,replace=TRUE),
check.names=FALSE)

df = setNames(SPSS_Data.df[,c("player.cumulative_donation",
"player.treatmentgroup","Session code")],
              c("Donation","Treatment","Session")
              )

model = lme(Donation ~ Treatment, random=~1|Session,data=df)
glht(model,linfct=mcp(Treatment="Tukey"))

Whereas when you put the variable into the environment, i get the same error:

Donation = df$Donation
Treatment = df$Treatment
Session =df$Session
model = lme(Donation ~ Treatment, random=~1|Session)
glht(model,linfct=mcp(Treatment="Tukey"))

Error in model.frame.lme(object) : object does not contain any data
Error in factor_contrasts(model) : 
  no ‘model.matrix’ method for ‘model’ found!
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • I think you are right with what you are saying! The problem is I get the Data (massive amount) from an excel file, so I import it straight away. However, I tried to convert my data to a dataframe using ```SPSS_Data.df <- as.data.frame(SPSS_Data)```. I use the exact variable name in the model function as above (player.treatmentgroup instead of SPSS_Data$player.treatmentgroup). However, I get an error in the mcp function: ```Error in mcp2matrix(model, linfct = linfct) : Variable(s) ‘player.treatmentgroup’ have been specified in ‘linfct’ but cannot be found in ‘model’``` – R_Beginner Feb 24 '20 at 15:22
  • hey, your column names are different from that in the model, hence it doesn't work. either you rename columns of SPSS_Data.df to Donation, Treatment etc, or you just use lme(player.cumulative_donation~player.treatmentgroup, random=~'Session code',data=..) – StupidWolf Feb 24 '20 at 15:30
  • I still seem to have something not figured out yet. 1. I converted my data to a dataframe (SPSS_Data.df), 2. I renamed the columns of the dataframe (Donation, Treatment, Session). 3. I created variables like this: Treatment <- SPSS_Data.df$player.treatmentgroup (which is weird since it still uses the old name). 4. I computed models, 5. still an error at glht. Sorry, I am a beginner and quite lost what may be wrong – R_Beginner Feb 24 '20 at 16:22
  • Done, again thanks for your help, I highly appreciate it!! – R_Beginner Feb 24 '20 at 16:31
  • Do you mean the one in your answer? It does work indeed, however there is no Std. Error, z value and Pr(>|z|) as shown in the link. Is this because of my data? – R_Beginner Feb 24 '20 at 17:08
  • Thats glht, you got to do summary(glht(model,linfct=mcp(Treatment="Tukey"))) Sorry I thought the original question was to do posthoc ? – StupidWolf Feb 24 '20 at 17:14
  • 1
    My bad, you're right. Thanks for your help, I think I manage to do the rest on my own! – R_Beginner Feb 24 '20 at 17:26