我是统计学的学生,我想请求一些帮助。我想在我的数据集中绘制预测值和100天课程的实际值:
示例数据:
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)
在100天内绘制实际值:
plot(count~days,type="l")
回归:
poisson <- glm(count ~mood+wind+temp)
我的预测因子和预测值的条件:
hyp<- c(1,1,3,20)
coeff.p1 <- poisson$coefficients
XB <- hyp%*%coeff.p1
predv.y <- exp(XB)
predv.y
可能有一种方法可以预测所有观察值,例如:
coeff.p1 <- poisson$coefficients
XB <- c(,2:4)%*%t(coeff.p1)
我打算乘以列2:4乘以
Error in c(, 2:4) : argument 1 is empty
我陷入了困境。因此,我想在一个地块上获得100天的预测值和实际值。
谢谢
答案 0 :(得分:2)
您的对象poisson
属于glm
类,因此在给定任何数据时它具有predict
方法。
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')
答案 1 :(得分:0)
试试这个:
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")