Plotting GLM predicted values with a categorical and a continuous predictor

Viewed 164

I have a generalised linear model in which one predictor is a categorical variable (binary 1/0) and one is a continuous variable.

Here's my data and my code:

library(reprex)
library(tidyverse)
library(datapasta)
library(ggplot2)

# my data frame: rainfall_binary is a binary vector (factor) while temp is continuous (it's temperature)

df <- data.frame(
  VH_average = c(-18.4527033816948,
                 -16.2644305598873,-16.1847107297772,-16.1971205524948,
                 -16.5239874732068,-17.2211302093816,-18.2875256347705,
                 -13.7489056675713,-14.5000673290099,-15.4042266341501,-15.1817907735829,
                 -15.6276170790517,-18.3260089724533,-15.1530169022796,
                 -16.1034334250424,-15.8582055282567,-16.2250640523659,
                 -16.9422869158206,-17.5646332225128,-15.4220277527455,
                 -15.7061506787604,-16.0053241375835,-15.6587460003135,
                 -16.848757403998,-17.9766074787419,-15.2396630934534,
                 -15.7573442344174,-15.8994493522684,-15.5906833828475,
                 -16.8160028280318),
  rainfall_binary = c(1,1,1,1,1,1,1,1,1,1,1,
                      1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0),
  temp = c(4.05,4.05,4.05,4.05,4.05,
           4.05,7,7,7,7,7,7,4.9,4.9,4.9,4.9,4.9,4.9,5.7,
           5.7,5.7,5.7,5.7,5.7,2.25,2.25,2.25,2.25,2.25,2.25)
)


# build model

GLM.REPREX <- glm(data=df,VH_average~rainfall_binary+temp)

# create predictors


df_newtemp <- 
  data.frame(temp= seq(0,20, length=100))


df_predicts <- predict(GLM.REPREX, newdata=df_newtemp, int = 'c')
#> Error in eval(predvars, data, env): oggetto "rainfall_binary" non trovato

Created on 2020-03-25 by the reprex package (v0.3.0)

It says that it doesn't find the object. I have tried in other ways, but I am new to R and I'm not even sure if what I'm doing is right. Any suggestions is appreciated! Thanks

0 Answers
Related