emudrak
emudrak

Reputation: 1016

lsmeans::lsmip: how to plot continuous values correctly?

The lsmip() command in the lsmeans package seems to treat continuous predictors on the x-axis as categorical predictors.

If I have observations from my continuous predictors at discrete but uneven intervals, (such as when you remove the 0.2 level of nitro from the Oats data), lsmip plots them at even intervals anyway.

data("Oats", package = "nlme")
Oats.lmer2sub <- lmer(log(yield) ~ Variety + poly(nitro,2)
               + (1|Block/Variety),  data = subset(Oats, nitro!=0.2))
lsmip(Oats.lmer2sub, Variety ~ nitro, ylab = "Predicted log(yield)", cov.reduce=FALSE)

enter image description here

This seems important to me if I want to show the model predictions at the levels of nitro where I actually observed the data. Theoretically, I should be able to show predicted values at any level of nitro.

lsmip(Oats.lmer2sub, Variety ~ nitro, ylab = "Predicted log(yield)", at=list(nitro=c(0, 0.2, 0.4, 0.45, 0.46,  0.6)))

enter image description here

Is there any way to set the x-axis to space the values of nitro to a scale appropriate for a continuous variable? Meaning skip a space for 0.2 in the first graph, or cluster together the points for 0.45, 0.46 in the second graph?

Upvotes: 1

Views: 1450

Answers (1)

Russ Lenth
Russ Lenth

Reputation: 6820

You can save the plotted data in a data frame, then plot those results however you want. In your example:

plotdf <- lsmip(Oats.lmer2sub, Variety ~ nitro, 
          at=list(nitro=c(0, 0.2, 0.4, 0.45, 0.46,  0.6)), 
          plotit = FALSE)

library(lattice)
xyplot(lsmean ~ nitro, groups = ~Variety, 
       type = "o", ylab = "Predicted log(yield)", data = plotdf)

Resulting plot

Upvotes: 1

Related Questions