Invalid argument in the lsmeans model?

194 views Asked by At

I am getting the error saying

lsmDrugs=lsmeans(model1, -drug) Error in -drug : invalid argument to unary operator"

Any help please?

library(knitr)
library(lsmeans)

drug <- c(rep("A", 2),rep("B", 2),rep("C", 2))
drugDiff <- c(-14, -4, 5, -1, -2, 6)

data<-data.frame(Drug=as.factor(drug),Difference=drugDiff)
data

model1 <- aov(drugDiff ~ drug, data = data)

anova(model1)
kable(anova(model1), format = "markdown")

lsmDrugs = lsmeans(model1, -drug)

summary(contrast(lsmDrugs, method="pairwise", 
                 adjust = "tukey"), infer = c(T, T), level = 0.95, side = "twosided")
1

There are 1 answers

0
Artem On

You have a typo as you should use tilde - ~ instead of - charachter. Tildes are used for formulas in R. Please see the code below:

library(knitr)
library(lsmeans)

drug <- c(
  rep("A", 2),
  rep("B", 2),
  rep("C", 2)
)

drugDiff <- c(-14, -4, 5, -1, -2, 6)

data <- data.frame(Drug = as.factor(drug), Difference = drugDiff)
data

model1 <- aov(drugDiff ~ drug, data = data)

anova(model1)
kable(anova(model1), format = "markdown")

lsmDrugs = lsmeans(model1, ~ drug) # tilde instead of minu, used for formula

summary(
  contrast(lsmDrugs, method = "pairwise",
           adjust = "tukey"),
  infer = c(T, T),
  level = 0.95,
  side = "twosided"
)

Output:

 contrast      estimate       SE df lower.CL upper.CL t.ratio p.value
 A - B    -1.100000e+01 5.773503  3 -35.1261  13.1261  -1.905  0.2817
 A - C    -1.100000e+01 5.773503  3 -35.1261  13.1261  -1.905  0.2817
 B - C    -1.776357e-15 5.773503  3 -24.1261  24.1261   0.000  1.0000

Confidence level used: 0.95 
Conf-level adjustment: tukey method for comparing a family of 3 estimates 
P value adjustment: tukey method for comparing a family of 3 estimates