title: K-fold cross-validation using cv.lm()
body: <p>I am new to R and trying to do K-fold cross validation using cv.lm()
  Refer: <a href="http://www.statmethods.net/stats/regression.html" rel="nofollow">http://www.statmethods.net/stats/regression.html</a></p>
  
  <p>I am getting error indicating the length of my variable are different. Infact during my verification using length(), I found the size in fact the same.</p>
  
  <p>The below are the minimal datasets to replicate the problem,</p>
  
  <pre><code>X   Y
  277 5.20
  285 5.17
  297 4.96
  308 5.26
  308 5.11
  263 5.27
  278 5.20
  283 5.16
  268 5.17
  250 5.20
  275 5.18
  274 5.09
  312 5.03
  294 5.21
  279 5.29
  300 5.14
  293 5.09
  298 5.16
  290 4.99
  273 5.23
  289 5.32
  279 5.21
  326 5.14
  293 5.22
  256 5.15
  291 5.09
  283 5.09
  284 5.07
  298 5.27
  269 5.19
  </code></pre>
  
  <p>Used the below code to do the cross-validation</p>
  
  <pre><code># K-fold cross-validation, with K=10
  sampledata &lt;- read.table("H:/sample.txt", header=TRUE)
  y.1 &lt;- sampledata$Y
  x.1 &lt;- sampledata$X
  fit=lm(y.1 ~ x.1)
  library(DAAG)
  cv.lm(df=sampledata, fit, m=10)
  </code></pre>
  
  <p>The error on the terminal,</p>
  
  <pre><code>Error in model.frame.default(formula = form, data = df[rows.in, ], drop.unused.levels = TRUE) : 
    variable lengths differ (found for 'x.1')
  </code></pre>
  
  <p>Verification,</p>
  
  <pre><code>&gt; length(x.1)
  [1] 30
  &gt; length(y.1)
  [1] 30
  </code></pre>
  
  <p>The above confirms the length are the same. </p>
  
  <pre><code>&gt; str(x.1)
   int [1:30] 277 285 297 308 308 263 278 283 268 250 ...
  &gt; str(y.1)
   num [1:30] 5.2 5.17 4.96 5.26 5.11 5.27 5.2 5.16 5.17 5.2 ...
  
  &gt; is(y.1)
  [1] "numeric" "vector" 
  &gt; is(x.1)
  [1] "integer"             "numeric"             "vector"              "data.frameRowLabels"
  </code></pre>
  
  <p>Further check on the data set as above indicates one dataset is integer and another is numeric. But even when the data sets are converted the numeric to integer or integer to numeric, the same error pops up in the screen indicating issues with data length.</p>
  
  <p>Can you guide me what should I do to correct the error? </p>
  
  <p>I am unsuccessful in handling this since 2 days ago. Did not get any good lead from my research using internet.</p>
  
  <p><strong>Addional Related Query:</strong></p>
  
  <p>I see the fit works if we use the headers of the data set in the attributes,</p>
  
  <pre><code>fit=lm(Y ~ X, data=sampledata)
  </code></pre>
  
  <p>a) what is the difference of the above syntax with,</p>
  
  <pre><code>fit1=lm(sampledata$Y ~ sampledata$X)
  </code></pre>
  
  <p>Thought it is the same. In the below,</p>
  
  <pre><code>#fit 1 works
  fit1=lm(Y ~ X, data=sampledata)
  cv.lm(df=sampledata, fit1, m=10)
  
  #fit 2 does not work
  fit2=lm(sampledata$Y ~ sampledata$X)
  cv.lm(df=sampledata, fit2, m=10)
  </code></pre>
  
  <p>The problem is at <em>df=sampledata</em> as the header "<em>sampledata$Y</em>" does not exist but only $Y exist. Tried to manupulate cv.lm to below it does not work too,</p>
  
  <pre><code>cv.lm(fit2, m=10)
  </code></pre>
  
  <p>b) How if we like to manipulate the variables, how to use it in cv.lm() for e.g</p>
  
  <pre><code>y.1 &lt;- (sampledata$Y/sampledata$X)
  x.1 &lt;- (1/sampledata$X)
  
  #fit 4 problem
  fit4=lm(y.1 ~ x.1)
  cv.lm(df=sampledata, fit4, m=10)
  </code></pre>
  
  <p>Is there a way I could reference y.1 and x.1 instead of the header Y ~ X in the function?</p>
  
  <p>Thanks.</p>