title: Cross-validating an ordinal logistic regression in R (using rpy2)
body: <p>I'm trying to create a predictive model in Python, comparing several different regression models through cross-validation. In order to fit an ordinal logistic model (<code>MASS.polr</code>), I've had to interface with R through <code>rpy2</code> as follows:</p>
  
  <pre><code>from rpy2.robjects.packages import importr
  import rpy2.robjects as ro
  
  df = pd.DataFrame()
  df = df.append(pd.DataFrame({"y":25,"X":7},index=[0]))
  df = df.append(pd.DataFrame({"y":50,"X":22},index=[0]))
  df = df.append(pd.DataFrame({"y":25,"X":15},index=[0]))
  df = df.append(pd.DataFrame({"y":75,"X":27},index=[0]))
  df = df.append(pd.DataFrame({"y":25,"X":12},index=[0]))
  df = df.append(pd.DataFrame({"y":25,"X":13},index=[0]))
  
  # Loads R packages. 
  base = importr('base')
  mass = importr('MASS')
  
  # Converts df to an R dataframe. 
  from rpy2.robjects import pandas2ri
  pandas2ri.activate()
  ro.globalenv["rdf"] = pandas2ri.py2ri(df) 
  
  # Makes R recognise y as a factor. 
  ro.r("""rdf$y &lt;- as.factor(rdf$y)""")
  
  # Fits regression. 
  formula = "y ~ X"    
  ordlog = mass.polr(formula, data=base.as_symbol("rdf"))
  ro.globalenv["ordlog"] = ordlog
  print(base.summary(ordlog))
  </code></pre>
  
  <p>So far, I have mainly been comparing my models using <code>sklearn.cross_validation.test_train_split</code> and <code>sklearn.metrics.accuracy_score</code>, yielding a number from 0 to 1 which represents the accuracy of the training-set model in predicting the test-set values.</p>
  
  <p>How might I replicate this test using <code>rpy2</code> and <code>MASS.polr</code>?</p>