0
votes

I have a data frame of bird counts. I have the participants ID number, the number of birds they counted, the year they counted them, their lat and long coordinates, and their effort. I have made this model:

model = lmer(count~year+lat+long+effort+(1|participant), data = df)

I now want the model to plot predicted values from that same data set. So, that data was for 1997-2017, and I want the model to give me predicted values for each year. I want to plot these, so the final plot will have the predicted count on the y-axis, and the year (categorical) on the x-axis. Each year will have one data point w/ a confidence interval.

I have tried figuring out predict(), but I'm not quite sure how to use that to get what I want. It seems to need a new data frame, but I don't have a new data set to run through the model to predict a future count. I want the model to go back and work on the previous data that I put into it already, based off of the Beta values in the output of summary(model).

I found this thread, and it seems to be basically what I'm looking to do, but I can't get the sjPlot dependencies to download, sjlabelled throws an error every time: How to plot predicted values with standard errors for lmer model results?

2
Standard errors are going to be hard, but take a look at visreg for quick predicted effects plot, for example visreg::visreg(model, "year"). Mind you that this will set continuous predictors to their median value, and factor variables to the most common one in order to get actual predictions for just year. - Remko Duursma
@RemkoDuursma Thanks for this. When I do this, though, I'm getting data points plotted into the negative values, though, which doesn't make sense since you can't have negative counts. Any advice? - Heliornis
Probably because you are plotting contrasts not predictions, try setting type='conditional'. - Remko Duursma
Interesting, but that's still not changing it unfortunately. - Heliornis
In that case it is difficult to tell without example data, sorry. - Remko Duursma

2 Answers

0
votes

You could try the ggeffects-package, which will be used in the forthcoming sjPlot-update to plot predicted values.

library(ggeffects)
dat <- ggpredict(model, terms = "dat")
plot(dat)

If you're missing dependencies, try:

install.packages(
  c("sjlabelled", "sjmisc", "sjstats", "ggeffects", "sjPlot"),
  dependencies = TRUE
)

You may even want to install ggeffects from GitHub, since the current dev-version has some fixes and improvements for mixed models.

devtools::install_github("strengejacke/ggeffects")
0
votes

I found the package I was looking for, it's called predictedmeans and has a function where you put in the model and the model term you want predictions for predictmeans(model, model term). It works perfectly!