title: Replicate least squares regression to check consistency of estimation and prediction with truth
body: <p>Here is the problem: Five observations on <code>Y</code> are to be taken when <code>X = 4, 8, 12, 16, 20</code>, respectively. The true regression function is <code>E(y) = 20 + 4X</code>, and the <code>ei</code> are independent <code>N(O, 25)</code>.</p>
  
  <ol>
  <li><p>Generate five normal random numbers, with mean 0 and variance 25. Consider these random numbers as the error terms for the five <code>Y</code> observations at <code>X = 4,8, 12, 16, 20</code> and calculate <code>Y1</code>, <code>Y2</code> , <code>Y3</code> , <code>Y4</code> , and <code>Y5</code>. Obtain the least squares estimates <code>bo</code> and <code>b1</code>, when fitting a straight line to the five cases. Also calculate <code>Yh</code> when <code>Xh = 10</code> and obtain a 95 percent confidence interval for <code>E(Yh)</code> when <code>Xh = 10</code>. I did part 1, but  I need help to repeat it for 200 times.  </p></li>
  <li><p>Repeat part (1) 200 times, generating new random numbers each time.</p></li>
  <li><p>Make a frequency distribution of the 200 estimates <code>b1</code>. Calculate the mean and standard deviation of the 200 estimates <code>b1</code>. Are the results consistent with theoretical expectations?</p></li>
  <li><p>What proportion of the 200 confidence intervals for <code>E(Yh)</code> when <code>Xh = 10</code> include <code>E(Yh)</code>? Is this result consistent with theoretical expectations?</p></li>
  </ol>
  
  <p>Here's my code so far, I am stumped on how to repeat part 1 for 200 times:</p>
  
  <pre><code>X &lt;- matrix(c(4, 8, 12, 16, 20), nrow = 5, ncol = 1)
  e &lt;- matrix(c(rnorm(5,0,sqrt(5))), nrow = 5, ncol = 1)
  Y &lt;- 20 + 4 * X + e
  mydata &lt;- data.frame(cbind(Y=Y, X=X, e=e))
  names(mydata) &lt;- c("Y","X","e")
  reg&lt;-lm(Y ~ X, data = mydata)
  predict(reg, newdata = data.frame(X=10), interval="confidence")
  </code></pre>