title: How to obtain cross-validated r-square from linear model in R?
body: <p>I have a linear model in R.</p>
  
  <pre><code>set.seed(1234)
  x &lt;- rnorm(100)
  z &lt;- rnorm(100)
  y &lt;- rnorm(100, x+z)
  mydata &lt;- data.frame(x,y,z)
  
  fit &lt;- lm(y ~ x + z, mydata)
  </code></pre>
  
  <p>I would like to obtain an estimate of the out of sample r-square. I was thinking of using some form k-fold cross validation.</p>
  
  <ul>
  <li>What code in R takes a linear model fit and returns a cross-validated r-square?</li>
  <li>Or is there some other approach to obtaining cross-validated r-square using R?</li>
  </ul>