library(ggplot2)
fish.data=data.frame(sitenumber=1:100,
fish.population=c(7, 3, 7, 1, 9, 1, 0, 0, 33, 15, 0, 8, 10, 6, 6, 0, 5, 4, 0, 0, 18, 1, 0, 4, 9, 35, 7, 6, 52, 18, 2, 6, 0, 1, 1, 1, 17, 0, 1, 0, 1, 2, 3, 2, 1, 5, 21, 5, 3, 2, 0, 2, 12, 4, 0, 0, 3, 0, 0, 2, 4, 10, 13, 10, 2, 4, 15, 3, 4, 20, 2, 1, 33, 0, 3, 0, 6, 32, 15, 0, 18, 5, 1, 1, 1, 0, 0, 52, 2, 23, 0, 4, 8, 4, 6, 13, 4, 0, 0, 3) ,
# Standardized food availability:
food=c(0.7726, 0.3052, 0.4091, -0.7749, 0.9618, -0.2377, -1.2015, -0.8639, 1.6418, 1.1006, -1.2936, 0.3786, 0.5588, 0.3441, 0.4137, -0.625, 0.5609, -0.2873, -0.7354, -2.353, 1.3576, 0.0096, -1.6573, 0.5267, 0.9805, 1.5259, 0.4241, 0.5559, 1.9227, 1.1616, -0.4877, 0.434, -1.3075, -1.2571, -0.4045, 0.3146, 1.0496, -1.1584, -0.9643, -0.7425, -0.6268, -0.2768, -0.0564, 0.0732, -0.4332, 0.7553, 1.2763, 0.5868, 0.0771, -0.2776, -0.1421, -0.5285, 0.8199, 0.11, -0.4764, -1.189, 0.3835, 0.1221, -0.327, -0.4722, 0.4809, 0.9963, 0.8434, 0.9199, -0.2348, 0.308, 1.0344, 0.0919, -0.0676, 1.317, -0.2041, -0.4657, 1.565, -0.8463, 0.457, -1.1507, 1.0512, 1.7268, 1.0127, -0.4697, 1.4311, 0.1237, -0.8073, -0.6794, -0.8374, -0.053, -1.2292, 1.8966, -0.3192, 1.4033, -0.6946, 0.1261, 1.1178, -0.0309, 0.9486, 0.9798, -0.4686, -1.9257, -1.4116, -0.1264) )
fish.res=glm(fish.population~food, family=poisson, data=fish.data)
food.new <- seq(min(fish.data$food), max(fish.data$food), length.out = 100) # Creating new values of the covariates for which we want to predict the response variable
resp.pred=data.frame(food=food.new)
resp.pred$pred <- predict(fish.res, newdata=resp.pred, type="response") # Predictions
pred.linkscale <- predict(fish.res, newdata=resp.pred, se=T)
pred.linkscale.CI2.5 = pred.linkscale$fit-1.96*pred.linkscale$se.fit
pred.linkscale.CI97.5 = pred.linkscale$fit+1.96*pred.linkscale$se.fit
resp.pred$CI2.5 = family(fish.res)$linkinv(pred.linkscale.CI2.5)
resp.pred$CI97.5 = family(fish.res)$linkinv(pred.linkscale.CI97.5)
ggplot() +
geom_point(aes(x=food,y=fish.population), data=fish.data) +
geom_line(aes(x= food,y=pred ),col="red",data=resp.pred) + # Fitted values
geom_ribbon(aes(x=food, ymin = CI2.5,ymax = CI97.5),
alpha=0.15,data=resp.pred) +
labs( title="Fish abundance depending on food resources",
subtitle = "Basic approach",
x="Food",
y=" Abundance") +
theme_minimal()