Reputation:
Im a student of statistics and i would like kindly request for some assistance. I would like to plot predicted values together with actual values of the course of 100 days in my dataset:
Sample Data:
set.seet(1010)
count<-rpois(100, lambda = 5)
mood<- rbinom(100, size = 1, prob = .7)
temp<-rnorm(100, mean = 20, sd = 5)
wind<-rbinom(100, size = 3, prob = .7)
days<-seq(1,100,by=1)
df<-data.frame(count,mood,temp,wind,days)
Plotting actual values during 100 days:
plot(count~days,type="l")
Regression:
poisson <- glm(count ~mood+wind+temp)
Condition on my predictors and otaining predicted values:
hyp<- c(1,1,3,20)
coeff.p1 <- poisson$coefficients
XB <- hyp%*%coeff.p1
predv.y <- exp(XB)
predv.y
May be there is a way to predict values for all observations as for example:
coeff.p1 <- poisson$coefficients
XB <- c(,2:4)%*%t(coeff.p1)
I intend to multiply with columns 2:4 by always get
Error in c(, 2:4) : argument 1 is empty
Here Im stuck. As a result I would like to obtain predicted values and actuall values for 100 day on one plot.
Thank you
Upvotes: 1
Views: 18923
Reputation: 1726
Try this:
set.seed(1010)
count<-rpois(100, lambda = 5)
mood<- rbinom(100, size = 1, prob = .7)
temp<-rnorm(100, mean = 20, sd = 5)
wind<-rbinom(100, size = 3, prob = .7)
days<-seq(1,100,by=1)
df<-data.frame(count,mood,temp,wind,days)
poisson <- glm(count ~ mood+wind+temp
, family = poisson() #specify your model type
, data=df)
# Calculate the predicted
phat.poisson <- predprob(poisson) # for each subj, prob of observing each value
phat.poisson.mn <- apply(phat.poisson, 2, mean) # mean predicted probs
#your plot of observed vs. predicted
hist(count, prob = TRUE, col = "grey60", breaks=seq(-0.5, 12.5, 1), xlab = "Counts",main = NULL, ylim=c(0, .20))
lines(x = seq(0, 12, 1), y = phat.poisson.mn, lty=2, lwd=2, col="red")
points(x = seq(0, 12, 1), y = phat.poisson.mn, cex=1, pch=16, col="red")
Upvotes: 0
Reputation: 2992
Your object poisson
is of class glm
, so it has a predict
method when given any data.
poisson <- glm(count ~mood+temp+ wind)
df$pred<-predict(poisson,df[,2:4])
plot(df$days,df$count)
lines(df$days, df$pred,type='l',col='blue')
Upvotes: 2