-1

Here is the code I used:

data<-read.table("YB.txt",header=T)
attach(data)
fit2<-glm(cbind(success,fail)~time*col,data=data,family=binomial)
summary(fit2)
predict.data<-as.data.frame(predict(fit2,newdata=temp.data,type="link",se=TRUE))
new.data<-cbind(temp.data,predict.data)
std<-qnorm(0.95/2+0.5)
new.data$ymin<-fit2$family$linkinv(new.data$fit-std*new.data$se)
new.data$ymax<-fit2$family$linkinv(new.data$fit+std*new.data$se)
new.data$fit<-fit2$family$linkinv(new.data$fit)
op<-cbind(success/(Neggs))
p<-ggplot(data,aes(x=time,y=op,fill=col,color=col))+geom_point()

p+geom_ribbon(data=new.data,aes(y=fit,ymin=ymin,ymax=ymax),alpha=0.1,linetype="dashed")+geom_line(data=new.data,aes(y=fit),linetype="solid")+labs(x="patatou",y="patata",title="patati")+theme_calc()+scale_color_manual(values=c("#CC6666", "#9999CC"))+labs(colour="Eggs color",linetype="Eggs color",shapes="Eggs color")

=> I got two beautiful prediction curves. However, my collected data start at 5 days and end at 13 days. I would like to have the curve for 0-5 days and after 13 days (i.e: to 20 days). In order to have a prediction of what I should get. So I tried this:

NewData<-as.matrix(cbind(time,col))
colnames<-(NewData)
colnames(NewData)<-c("time","col")
 predict(fit2,NewData,se.fit=TRUE,scale=NULL,df=Inf,interval=c("none","confidence","prediction"),level=0.95)

Didn't work... Somebody have an idea to solve this?

Francois
  • 1
  • 3
  • I can't tell because I can't see the data, but here's some suggestions: 1) Be careful with extrapolation and make sure the answers make sense. 2) Filter the time frame to include only days 5-13 for your model. 3) On your `predict` line, use a new data frame that includes days 0-20. – Brian Balzar Jul 22 '18 at 18:05

1 Answers1

0

predict() uses a fitted model to provide you with the values of the y-variable that correspond to the values of the x-variables in the newdata argument. So if you only provide x-variable values that range from 5 to 13, you will only get the corresponding predicted y-variable values. In order to "extend" the prediction line, you need to supply x-variable values over the whole range that you want to plot, e.g., 0 to 20. You will want something like:

x_coords <- seq(from=0, to=20, by=0.1)
y_coords <- predict(fitted_model, newdata=data.frame(x=x_coords))
plot(x, y, xlim=c(0,20))
points(x=x_coords, y=y_coords, type="l")

My answer here (link) provides a worked example using the Auto dataset from the ISLR package.

DanY
  • 5,920
  • 1
  • 13
  • 33
  • @Francois - email me your code and data and I'll take a look - my profile has a link to my contact info – DanY Jul 22 '18 at 19:56