0

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?

Heliornis
  • 391
  • 5
  • 18
  • 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 Sep 14 '17 at 04:02
  • @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 Sep 14 '17 at 04:19
  • Probably because you are plotting contrasts not predictions, try setting `type='conditional'`. – Remko Duursma Sep 14 '17 at 06:29
  • Interesting, but that's still not changing it unfortunately. – Heliornis Sep 14 '17 at 12:45
  • In that case it is difficult to tell without example data, sorry. – Remko Duursma Sep 15 '17 at 01:37

2 Answers2

0

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")
Daniel
  • 7,252
  • 6
  • 26
  • 38
0

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!

Heliornis
  • 391
  • 5
  • 18