#!/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~1',
'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)
#dfr = com.convert_to_r_dataframe(mydata, True) # convert from pandas to R and make string columns factors
mydata['ID'] = mydata['ID'].astype('S8')
dfr = com.convert_to_r_dataframe(mydata, True) # convert from pandas to R and make string columns factors
print (base.levels(dfr.rx2('ID')))
[1] "1" "10" "2" "3" "4" "5" "6" "7" "8" "9"
res = []
for formula in formulae:
print 'Starting :'+str(formula)[:-1]
#lme_res = nlme.lme(fixed=formula, data=dfr, random=randoms[0], method='ML')
lme_res = lme4.lmer(formula=formula, data=dfr, REML='false')
#~ lme_res = base.summary(lme_res)
res.append(lme_res)
print 'Completed!'
Starting :RT ~ 1 + (1 | ID) Completed! Starting :RT ~ cat1 + (1 | ID) Completed! Starting :RT ~ cat1 + cat2 + (1 | ID) Completed!
res_sum = base.summary(res[0])
print res_sum
Linear mixed model fit by maximum likelihood ['lmerMod'] Formula: RT ~ 1 + (1 | ID) Data: structure(list(ID = structure(c(1L, 3L, 1L, 3L, 4L, 4L, 5L, 4L, 1L, 6L, 5L, 7L, 3L, 8L, 6L, 7L, 9L, 10L, 4L, 5L, 6L, 7L, 8L, 2L, 9L, 8L, 9L, 10L, 2L, 2L), .Label = c("1", "10", "2", "3", "4", "5", "6", "7", "8", "9"), class = "factor"), RT = c(3L, 3L, 6L, 6L, 3L, 3L, 3L, 6L, 1L, 3L, 6L, 3L, 9L, 3L, 6L, 6L, 3L, 3L, 9L, 9L, 9L, 9L, 6L, 3L, 6L, 9L, 9L, 9L, 9L, 6L), cat1 = c(1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 3L, 1L, 2L, 1L, 3L, 1L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 1L, 2L, 3L, 3L, 3L, 3L, 2L), cat2 = c(0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 1L)), .Names = c("ID", "RT", "cat1", "cat2"), row.names = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29"), class = "data.frame") AIC BIC logLik deviance 148.1946 152.3982 -71.0973 142.1946 Random effects: Groups Name Variance Std.Dev. ID (Intercept) 0.000 0.000 Residual 6.699 2.588 Number of obs: 30, groups: ID, 10 Fixed effects: Estimate Std. Error t value (Intercept) 5.6333 0.4725 11.92
latex = texreg.extract(res_sum)
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘extract’ for signature ‘"summary.merMod"’
--------------------------------------------------------------------------- RRuntimeError Traceback (most recent call last) <ipython-input-34-51a4f7034c33> in <module>() ----> 1 latex = texreg.extract(res_sum) /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.extract.lme4(res_sum)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-35-4b9a09fe7335> in <module>() ----> 1 latex = texreg.extract.lme4(res_sum) AttributeError: 'SignatureTranslatedFunction' object has no attribute 'lme4'
latex = texreg.extract.extract.lme4(res_sum)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-36-0ebfc1aa0b06> in <module>() ----> 1 latex = texreg.extract.extract.lme4(res_sum) AttributeError: 'SignatureTranslatedFunction' object has no attribute 'extract'
latex = texreg.texreg.extract.lme4(res_sum)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-37-a3de8471f3af> in <module>() ----> 1 latex = texreg.texreg.extract.lme4(res_sum) AttributeError: 'SignatureTranslatedFunction' object has no attribute 'extract'
comparison = stats.anova(*res[:2])
# comparison = base.summary(cmparison)
Error in Ops.data.frame(data, data[[1]]) : list of length 2 not meaningful In addition: Warning message: In anova.merMod(<S4 object of class "lmerMod">, <S4 object of class "lmerMod">) : failed to find unique model names, assigning generic names
--------------------------------------------------------------------------- RRuntimeError Traceback (most recent call last) <ipython-input-117-c965314c3d3f> in <module>() ----> 1 comparison = stats.anova(*res[:2]) 2 # comparison = base.summary(cmparison) /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 Ops.data.frame(data, data[[1]]) : list of length 2 not meaningful