#!/usr/bin/env python
import pandas.rpy.common as com
import pandas as pd
import numpy as np
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
nlme = importr('nlme')
lme4 = importr('lme4')
base = com.importr('base')
stats = com.importr('stats')
xtable = importr('xtable')
stargazer = importr('stargazer')
texreg = importr('texreg')
#formulae strings:
formulae = [
'RT~cat2',
'RT~cat1',
'RT~cat1+cat2',
]
randoms = [
'~1|ID'
]
for idx, formula in enumerate(formulae):
formulae[idx] = robjects.Formula(formula + ' + (1|ID)') # make a formula obect
#formulae[idx] = robjects.Formula(formula) # make a formula obect
for idx, random in enumerate(randoms):
randoms[idx] = robjects.Formula(random) # make a formula obect
mydata = pd.DataFrame.from_csv('http://chymera.eu/data/test/r_data.csv', parse_dates=False, index_col=False)
mydata['ID'] = mydata['ID'].astype('S8')
dfr = com.convert_to_r_dataframe(mydata, True) # convert from pandas to R and make string columns factors
res = []
for formula in formulae:
#lme_res = nlme.lme(fixed=formula, data=dfr, random=randoms[0], method='ML')
lme_res = lme4.lmer(formula=formula, data=dfr, REML='false')
res.append(lme_res)
res_sum = [base.summary(i) for i in res]
latex = texreg.texreg(res_sum[0], return_string = "TRUE")
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘extract’ for signature ‘"summary.merMod"’ In addition: There were 29 warnings (use warnings() to see them)
--------------------------------------------------------------------------- RRuntimeError Traceback (most recent call last) <ipython-input-53-913ac280f89c> in <module>() 1 res_sum = [base.summary(i) for i in res] ----> 2 latex = texreg.texreg(res_sum[0], return_string = "TRUE") /usr/lib64/python2.7/site-packages/rpy2/robjects/functions.pyc in __call__(self, *args, **kwargs) 84 v = kwargs.pop(k) 85 kwargs[r_k] = v ---> 86 return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs) /usr/lib64/python2.7/site-packages/rpy2/robjects/functions.pyc in __call__(self, *args, **kwargs) 33 for k, v in kwargs.iteritems(): 34 new_kwargs[k] = conversion.py2ri(v) ---> 35 res = super(Function, self).__call__(*new_args, **new_kwargs) 36 res = conversion.ri2py(res) 37 return res RRuntimeError: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘extract’ for signature ‘"summary.merMod"’
latex = texreg.texreg(res[0], return_string = "TRUE")
Computing profile confidence intervals ... Computing confidence intervals at a confidence level of 0.95. Use argument "method = 'boot'" for bootstrapped CIs. \begin{table} \begin{center} \begin{tabular}{l c } \hline & Model 1 \\ \hline (Intercept) & $5.27^{*}$ \\ & $[3.93;\ 6.61]$ \\ cat2 & $0.73$ \\ & $[-1.16;\ 2.63]$ \\ \hline AIC & 149.59 \\ BIC & 155.19 \\ Log Likelihood & -70.79 \\ Deviance & 141.59 \\ Num. obs. & 30 \\ Num. groups: ID & 10 \\ Variance: ID.(Intercept) & 0.00 \\ Variance: Residual & 6.56 \\ \hline \multicolumn{2}{l}{\scriptsize{$^*$ 0 outside the confidence interval}} \end{tabular} \caption{Statistical models} \label{table:coefficients} \end{center} \end{table}