title: R: Plot predicted and actual response over a time variable
body: <p>I have a model where I would like to plot the prediction over a time variable. Adding the average response for that time point in the same graph would also be very helpful.</p>
  
  <p>Here is some reproducible data.</p>
  
  <pre><code>set.seed(123)
  x1 = rnorm(1000)           # some continuous variables 
  x2 = rnorm(1000)
  z = 1 + 2*x1 + 3*x2        # linear combination with a bias
  pr = 1/(1+exp(-z))         # pass through an inv-logit function
   y = rbinom(1000,1,pr)      # bernoulli response variable
  
  #valid glm:
  df = data.frame(y=y,x1=x1,x2=x2,time=rep(seq(1:10),10))
  fit = glm( y~x1+x2,data=df,family="binomial")
  </code></pre>
  
  <p>Now I would like to plot <code>mean(predict(fit,df,type="response"))</code> by the group <code>time</code> as well as <code>mean(y)</code> by the group <code>time</code>.</p>
  
  <p>Any hints or ideas?</p>
  
  <p>EDIT: Thank you for the responses! Yes I am aware that in this example time is not in the model. I just wanted to make an easy example. In my real model, time is included. And yes I want to plot the mean response and mean prediction over time.</p>