adamhaber / pybrms

Pythonic interface to R's brms
Apache License 2.0
44 stars 7 forks source link

Errors with pybrms #3

Open johnsonrd58 opened 4 years ago

johnsonrd58 commented 4 years ago

commands not recognized

epilepsy = pybrms.get_brms_data("epilepsy")


AttributeError Traceback (most recent call last)

in ----> 1 epilepsy = pybrms.get_brms_data("epilepsy") AttributeError: module 'pybrms' has no attribute 'get_brms_data'
adamhaber commented 4 years ago

Thanks for reporting this! I'm planning to re-arrange the code to a proper package in the next following days, and I'll fix this along the way. I'll post here when its done.

johnsonrd58 commented 4 years ago

I very much appreciate this. Current updates with stan on R have created some havoc with windows 10 users as stan is failing at the compilation stage. Efforts are being employed to fix it. I am a huge brms user and am fluent in both python and R but better at R since I use it more. BRMS has made Bayesian statistics more amenable for users and your package provides options for the user in their platform choice.

Fantastic!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Robert D. Johnson, Ph.D [cid:image001.png@01D65F3F.DD71F620] Procter & Gamble Mason Business Center 8700 Mason Montgomery Road johnson.rd.1@pg.commailto:johnson.rd.1@pg.com (513) 634-9827

COVID19: I wish it need not have happened in my time and So do all who live to see such times. All we have to decide is what to do with the time that is given us

From: Adam Haber notifications@github.com Sent: Tuesday, July 21, 2020 7:31 AM To: adamhaber/pybrms pybrms@noreply.github.com Cc: Johnson, Robert johnson.rd.1@pg.com; Author author@noreply.github.com Subject: Re: [adamhaber/pybrms] Errors with pybrms (#3)

Thanks for reporting this! I'm planning to re-arrange the code to a proper package in the next following days, and I'll fix this along the way. I'll post here when its done.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/adamhaber/pybrms/issues/3#issuecomment-661800613, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AD6MJ6BUGRFNDVFK4324F2DR4V36BANCNFSM4PCVOEAA.

adamhaber commented 4 years ago

Hi, I'm trying to better understand the problem so I can try and fix this. How did you install the library?

sreedta8 commented 3 years ago

I ran into same error that started this thread:

Operating System: Ubuntu 20.04 using KDE Plasma Python Distribution: Anaconda 64-bit

I created a new environment "bayesian" and installed pymc3, pystan, bambi among others relevant for building hierarchical Bayesian models. I could run bambi and pymc3 successfully. Then I used "pip install pybrms" to install pybrms togo along wth rpy2-3.3.5 and other dependencies

Then I typed the following commands:

import pybrms # no error when I run this statement

epilepsy = pybrms.get_brms_data("epilepsy") # I got the same error as @pkpddude

Hope you can help resolve - I love brms and love the idea of using brms in Python

Sree

adamhaber commented 3 years ago

What happens if you do

from pybrms import pybrms

instead of

import pybrms

?

sreedta8 commented 3 years ago

Thanks for the quick reply. I get these long set of errors with the new command: (I have posted the entire output)

WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: Warning: WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: failed to download mirrors file (long vectors not supported yet: libcurl.c:952); using local file '/home/sreezach/anaconda3/envs/bayesian/lib/R/doc/CRAN_mirrors.csv'

WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: Error in unlink(f) : 3 arguments passed to .Internal(unlink) which requires 4


PackageNotInstalledError Traceback (most recent call last) ~/anaconda3/envs/bayesian/lib/python3.8/site-packages/pybrms/pybrms.py in 17 try: ---> 18 brms = rpackages.importr("brms") 19 except:

~/anaconda3/envs/bayesian/lib/python3.8/site-packages/rpy2/robjects/packages.py in importr(name, lib_loc, robject_translations, signature_translation, suppress_messages, on_conflict, symbol_r2python, symbol_resolve, data) 469 if not isinstalled(name, lib_loc=lib_loc): --> 470 raise PackageNotInstalledError( 471 'The R package "%s" is not installed.' % name

PackageNotInstalledError: The R package "brms" is not installed.

During handling of the above exception, another exception occurred:

RRuntimeError Traceback (most recent call last)

in ----> 1 from pybrms import pybrms ~/anaconda3/envs/bayesian/lib/python3.8/site-packages/pybrms/pybrms.py in 19 except: 20 utils = rpackages.importr("utils") ---> 21 utils.chooseCRANmirror(ind=1) 22 utils.install_packages(StrVector(('brms',))) 23 brms = rpackages.importr("brms") ~/anaconda3/envs/bayesian/lib/python3.8/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) 195 v = kwargs.pop(k) 196 kwargs[r_k] = v --> 197 return (super(SignatureTranslatedFunction, self) 198 .__call__(*args, **kwargs)) 199 ~/anaconda3/envs/bayesian/lib/python3.8/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) 123 else: 124 new_kwargs[k] = conversion.py2rpy(v) --> 125 res = super(Function, self).__call__(*new_args, **new_kwargs) 126 res = conversion.rpy2py(res) 127 return res ~/anaconda3/envs/bayesian/lib/python3.8/site-packages/rpy2/rinterface_lib/conversion.py in _(*args, **kwargs) 42 def _cdata_res_to_rinterface(function): 43 def _(*args, **kwargs): ---> 44 cdata = function(*args, **kwargs) 45 # TODO: test cdata is of the expected CType 46 return _cdata_to_rinterface(cdata) ~/anaconda3/envs/bayesian/lib/python3.8/site-packages/rpy2/rinterface.py in __call__(self, *args, **kwargs) 622 error_occured)) 623 if error_occured[0]: --> 624 raise embedded.RRuntimeError(_rinterface._geterrmessage()) 625 return res 626 RRuntimeError: Error in unlink(f) : 3 arguments passed to .Internal(unlink) which requires 4
sreedta8 commented 3 years ago

Just a thought:

I have R 4.0.2 installed outside of Anaconda and I noticed that the rpy2 is using a r-base which is 3.6.1 - Is there any potential conflict here? My understanding says "no",but one could never be sure

adamhaber commented 3 years ago

Can you try running

import rpy2.robjects.packages as rpackages
from rpy2.robjects import StrVector
utils = rpackages.importr("utils")
utils.install_packages(StrVector(('brms',)))
brms = rpackages.importr("brms")

in the same conda environment?

If this succeeds, try

from pybrms import pybrms

epilepsy = pybrms.get_brms_data("epilepsy")
sreedta8 commented 3 years ago

Get the same error as before:

--- Please select a CRAN mirror for use in this session --- WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: Warning: WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: failed to download mirrors file (long vectors not supported yet: libcurl.c:952); using local file '/home/sreezach/anaconda3/envs/bayesian/lib/R/doc/CRAN_mirrors.csv'

WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: Error in unlink(f) : 3 arguments passed to .Internal(unlink) which requires 4


RRuntimeError Traceback (most recent call last)

in 2 from rpy2.robjects import StrVector 3 utils = rpackages.importr("utils") ----> 4 utils.install_packages(StrVector(('brms',))) 5 brms = rpackages.importr("brms") ~/anaconda3/envs/bayesian/lib/python3.8/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) 195 v = kwargs.pop(k) 196 kwargs[r_k] = v --> 197 return (super(SignatureTranslatedFunction, self) 198 .__call__(*args, **kwargs)) 199 ~/anaconda3/envs/bayesian/lib/python3.8/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) 123 else: 124 new_kwargs[k] = conversion.py2rpy(v) --> 125 res = super(Function, self).__call__(*new_args, **new_kwargs) 126 res = conversion.rpy2py(res) 127 return res ~/anaconda3/envs/bayesian/lib/python3.8/site-packages/rpy2/rinterface_lib/conversion.py in _(*args, **kwargs) 42 def _cdata_res_to_rinterface(function): 43 def _(*args, **kwargs): ---> 44 cdata = function(*args, **kwargs) 45 # TODO: test cdata is of the expected CType 46 return _cdata_to_rinterface(cdata) ~/anaconda3/envs/bayesian/lib/python3.8/site-packages/rpy2/rinterface.py in __call__(self, *args, **kwargs) 622 error_occured)) 623 if error_occured[0]: --> 624 raise embedded.RRuntimeError(_rinterface._geterrmessage()) 625 return res 626 RRuntimeError: Error in unlink(f) : 3 arguments passed to .Internal(unlink) which requires 4
adamhaber commented 3 years ago

According to the last note here, removing utils.chooseCRANmirror(ind=1) (which caused the error in the original post) should have sent you to an interactive session in which you could choose the mirror from which you download the package.

If I understand it correctly, you got --- Please select a CRAN mirror for use in this session --- but then it crashed before you actually got to choose?

Anyway, this seems RPy2-related; I opened an issue on their repo, hope they can help.

adamhaber commented 3 years ago

@sreedta8 - following the answer on https://github.com/rpy2/rpy2/issues/735, can you try to:

  1. pip uninstall rpy2
  2. conda install -c r r-base
  3. conda install -c r rpy2 (or pip install rpy2 if reinstalling with conda doesn't help)?
sreedat commented 3 years ago

Thank you so much for the diligent and continued follow-up @adamhaber . I will try that and let you know asap.

Sree

sreedat commented 3 years ago

Hi I still the same error after I recreatedand built a new environment from scratch. Do you know what the exact dependenciesfor rpy2 are? I tried and failed to locate. I'm wondering if it is a Python version issue!

Please select a CRAN mirror for use in this session --- R[write to console]: Warning: R[write to console]: failed to download mirrors file (long vectors not supported yet: libcurl.c:952); using local file '/home/sreezach/anaconda3/envs/bayesian/lib/R/doc/CRAN_mirrors.csv'

R[write to console]: Error in unlink(f) : 3 arguments passed to .Internal(unlink) which requires 4


RRuntimeError Traceback (most recent call last)

in 1 utils = rpackages.importr("utils") ----> 2 utils.install_packages(StrVector(('brms',))) 3 brms = rpackages.importr("brms") ~/anaconda3/envs/bayesian/lib/python3.7/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) 196 kwargs[r_k] = v 197 return (super(SignatureTranslatedFunction, self) --> 198 .__call__(*args, **kwargs)) 199 200 ~/anaconda3/envs/bayesian/lib/python3.7/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) 123 else: 124 new_kwargs[k] = conversion.py2rpy(v) --> 125 res = super(Function, self).__call__(*new_args, **new_kwargs) 126 res = conversion.rpy2py(res) 127 return res ~/anaconda3/envs/bayesian/lib/python3.7/site-packages/rpy2/rinterface_lib/conversion.py in _(*args, **kwargs) 42 def _cdata_res_to_rinterface(function): 43 def _(*args, **kwargs): ---> 44 cdata = function(*args, **kwargs) 45 # TODO: test cdata is of the expected CType 46 return _cdata_to_rinterface(cdata) ~/anaconda3/envs/bayesian/lib/python3.7/site-packages/rpy2/rinterface.py in __call__(self, *args, **kwargs) 622 error_occured)) 623 if error_occured[0]: --> 624 raise embedded.RRuntimeError(_rinterface._geterrmessage()) 625 return res 626 RRuntimeError: Error in unlink(f) : 3 arguments passed to .Internal(unlink) which requires 4
sreedat commented 3 years ago

Hi Adam

I tested the following example of calling R from rpy2 and it worked (the code creates a plot)

import rpy2.robjects as robjects

r = robjects.r

x = robjects.IntVector(range(10)) y = r.rnorm(10)

r.X11()

r.layout(r.matrix(robjects.IntVector([1,2,3,2]), nrow=2, ncol=2)) r.plot(r.runif(10), y, xlab="runif", ylab="foo/bar", col="red")

I've installed r-base (3.6.1) and rpy2-3.3.6 - so I do not understand why installation of brms is not working - I now think it has to do with the R repositories

sreedat commented 3 years ago

Hi Adam @adamhaber

An additional code I ran is the following code from rpy2 website and it did not work. I got the same error I have been getting as before:

import rpy2's package module import rpy2.robjects.packages as rpackages

import R's utility package utils = rpackages.importr('utils')

select a mirror for R packages utils.chooseCRANmirror(ind=1) # select the first mirror in the list

Main error R[write to console]: Warning: R[write to console]: failed to download mirrors file (long vectors not supported yet: libcurl.c:952); using local file '/home/user/anaconda3/envs/bayesian/lib/R/doc/CRAN_mirrors.csv'

R[write to console]: Error in unlink(f) : 3 arguments passed to .Internal(unlink) which requires 4

sreedat commented 3 years ago

I tried one other piece of code from rpy2 website: same error @adamhaber

new code R package names packnames = ('ggplot2', 'brms')

R vector of strings from rpy2.robjects.vectors import StrVector

Selectively install what needs to be install. We are fancy, just because we can. names_to_install = [x for x in packnames if not rpackages.isinstalled(x)] if len(names_to_install) > 0: utils.install_packages(StrVector(names_to_install))

Error

R[write to console]: Warning: R[write to console]: failed to download mirrors file (long vectors not supported yet: libcurl.c:952); using local file '/home/user/anaconda3/envs/bayesian/lib/R/doc/CRAN_mirrors.csv'

R[write to console]: Error in unlink(f) : 3 arguments passed to .Internal(unlink) which requires 4


RRuntimeError Traceback (most recent call last)

in 9 names_to_install = [x for x in packnames if not rpackages.isinstalled(x)] 10 if len(names_to_install) > 0: ---> 11 utils.install_packages(StrVector(names_to_install)) ~/anaconda3/envs/bayesian/lib/python3.7/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) ~/anaconda3/envs/bayesian/lib/python3.7/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) ~/anaconda3/envs/bayesian/lib/python3.7/site-packages/rpy2/rinterface_lib/conversion.py in _(*args, **kwargs) ~/anaconda3/envs/bayesian/lib/python3.7/site-packages/rpy2/rinterface.py in __call__(self, *args, **kwargs) RRuntimeError: Error in unlink(f) : 3 arguments passed to .Internal(unlink) which requires 4
sreedat commented 3 years ago

@adamhaber I noticed in the rpy2 website that it compiles using Python 3.5 and R 3.2+. See the link here:

https://rpy2.github.io/doc/v2.9.x/html/overview.html#background

See the section under Requirements

I'm working with Python 3.8 and Python 3.7.9 and I get the following inconsistency error when I run

conda install -c r rpy2

*Found conflicts! Looking for incompatible packages. This can take several minutes. Press CTRL-C to abort. failed

UnsatisfiableError: The following specifications were found to be incompatible with the existing python installation in your environment:

Specifications:

Your python: python=3.8

If python is on the left-most side of the chain, that's the version you've asked for. When python appears to the right, that indicates that the thing on the left is somehow not available for the python version you are constrained to. Note that conda will not change your python version to a different minor version unless you explicitly specify that.*

Could this be the source of errors I'm seeing? I'm curious since as I reported earlier, I can get rpy2 to work as long as I do not ask it to install any packages.

pip install rpy2 works and it does not give the unsatisfiable error, but it does not let me install brms or anyother package.

adamhaber commented 3 years ago

Thanks for running all these tests! Hopefully this will help in improving packaging/dependencies issues.

Could this be the source of errors I'm seeing? I'm curious since as I reported earlier, I can get rpy2 to work as long as I do not ask it to install any packages.

Not sure, these things can require a lot of trial-and-error sometimes... :-\ Especially with complicated dependencies like R and RPy2.

Can you try creating a new conda environment with Python 3.6?


pip uninstall pybrms
pip uninstall rpy2
conda create -n foo python=3.6
conda activate foo
conda install -c r r-base
conda install -c r rpy2
pip install pybrms
sreedat commented 3 years ago

@adamhaber I'm doing that now. I will let you know how it proceeds

sreedat commented 3 years ago

@adamhaber I just got these 2 errors towards the end of the installation process: I will still test it though and let you know. One is for pip and the other is for rpy2 and pybrms

*ERROR: After October 2020 you may experience errors when installing or updating packages. This is because pip will change the way that it resolves dependency conflicts.

We recommend you use --use-feature=2020-resolver to test your packages with the new resolver before it becomes the default.*

pybrms 0.0.31 requires rpy2>=3.1, but you'll have rpy2 2.9.4 which is incompatible.

adamhaber commented 3 years ago

I just tried

pip uninstall pybrms
pip uninstall rpy2
conda create -n foo python=3.6
conda activate foo
conda install -c r r-base
conda install -c r rpy2
pip install pybrms

conda installed RPy2 3.1.0, despite the fact that this site also lists v2.9.4, and I was able to use pybrms. What happens if you try conda install -c r rpy2==3.1.0?

EDIT: Two more suggestions/questions:

  1. What happens if, after conda install -c r r-base, you try to pip install rpy2?
  2. Have you tried this? My guess is that it's not the reason (since you were able to call R in general), but might be worth trying.
sreedta8 commented 3 years ago

@adamhaber By default conda installed rpy2 v-2.9.4. Then I used pip install rpy2 v-3.3.6 and pip deleted the old version and installed the new one. Sorry about some of the large bolded font. I do not how that happens to undo that. My apologies

I was able to execute the commands but the brms installation has gotten stuck with the inability to install "curl" package with the following error:

installing source package ‘curl’ ... package ‘curl’ successfully unpacked and MD5 sums checked using staged installation Found pkg-config cflags and libs! Using PKG_CFLAGS=-I/usr/include/x86_64-linux-gnu Using PKG_LIBS=-lcurl ------------------------- ANTICONF ERROR --------------------------- Configuration failed because libcurl was not found. Try installing: deb: libcurl4-openssl-dev (Debian, Ubuntu, etc) rpm: libcurl-devel (Fedora, CentOS, RHEL) csw: libcurl_dev (Solaris) If libcurl is already installed, check that 'pkg-config' is in your PATH and PKG_CONFIG_PATH contains a libcurl.pc file. If pkg-config is unavailable you can set INCLUDE_DIR and LIB_DIR manually via: R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'

When I tried to run sudo apt install libcurl4-openssl-dev I got the following message

libcurl4-openssl-dev is already the newest version (7.68.0-1ubuntu2.2). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Searching on Stack Overflow users suggested that curl needs to be manually compiled if R in a conda environment is unable to recognize libcurl4 ( https://stackoverflow.com/questions/42115972/configuration-failed-because-libcurl-was-not-found )

So I used the code given there (I'm a beginner with Linux / Unix env)

sudo wget https://github.com/curl/curl/releases/download/curl-7_72_0/curl-7.72.0.tar.gz ./configure make make install

In the new environment in miniconda3 where I installed rpy2 I checked that the file libcurl.pc was present in the pkgconfig folder and it is, but I'm unable to install brms.

I can install all other packages that do not depend on curl but not V8, rsconnect, rstan, brms, shinystan that depend on curl

I'm stuck at this point now. Let me try what you suggested of conda rpy2 with request for version 3.1 - thanks again for your help

sreedta8 commented 3 years ago

@adamhaber

I tried the steps to install rpy2==3.1.0 using conda - that installation worked but not the brms installation

Got the same curl CONFIG error - All those not dependent on curl were installed

Found pkg-config cflags and libs! Using PKG_CFLAGS=-I/usr/include/x86_64-linux-gnu Using PKG_LIBS=-lcurl ------------------------- ANTICONF ERROR --------------------------- Configuration failed because libcurl was not found. Try installing: deb: libcurl4-openssl-dev (Debian, Ubuntu, etc) rpm: libcurl-devel (Fedora, CentOS, RHEL) csw: libcurl_dev (Solaris) If libcurl is already installed, check that 'pkg-config' is in your PATH and PKG_CONFIG_PATH contains a libcurl.pc file. If pkg-config is unavailable you can set INCLUDE_DIR and LIB_DIR manually via: R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'

I will test some more later

adamhaber commented 3 years ago

Have you tried conda install -c anaconda libcurl?

sreedta8 commented 3 years ago

Yes I have tried both anaconda curl and libcurl- still the same error persists

sreedta8 commented 3 years ago

@adamhaber

I solved the curl error by adding the location of libcurl.pc in miniconda3 to the PKG_CONFIG_PATH and could install curl - Now I'm having issues with V8 package upon which all Stan based packages work. I will let you know more as I find the resolution

adamhaber commented 3 years ago

Wow, great work @sreedta8 ! I wonder to what extent are these issues OS-specific. Would be happy to hear if you make any progress with this.

sreedta8 commented 3 years ago

@adamhaber I got pybrms working using r-base 3.6.1, rpy2-3.3.6, and python 3.6.10 on a Windows 10 Anaconda platform with some simple tweaks. I'm on the final steps of getting it to work on Ubuntu. Fingers-crossed and a few prayers under my breath. I will let you know how it goes. One thing is for sure: the installation for Windows and Ubuntu is very very different and it has to do with how the systems are setup. Making it work under Anaconda is also another big additional challenge. I will pull all of my notes together and send some thing to you so you add this to your site - Thank you so much!

adamhaber commented 3 years ago

Happy to hear that worked out for you, and thanks for all your efforts! I hope you'll find it useful. It's still in its infancy, so any feedback regarding actual usage (other than dependencies struggles) would be much appreciated.

sreedta8 commented 3 years ago

@adamhaber I also got it working on Ubuntu with the help of @jeroen (Jeroen Ooms) who maintains the V8 library on GitHub. He had asked me to install r-v8 from conda-forge and that solved all the issues I faced with getting curl, V8 etc to work correctly in a conda environment on Ubuntu. Thanks for your help. Having you correspond and reading your input helped out a lot. In about a week, I will send the documentation that you can use / post here. I will be testing the pybrms for model evaluation and visualiztions with arviz next week. I will provide my feedback.

Sree

sreedta8 commented 3 years ago

@adamhaber: From the Windows 10 Anaconda 64-bit Here is the brms output from the Epilepsy data Poisson model: (The conda env by default targets MSVC C++ compiler. According to Pystan documentation MSVC does not work. So I used the following (from here https://pystan.readthedocs.io/en/latest/windows.html)

conda install libpython m2w64-toolchain -c msys2 and got pybrms and brms to work

Iteration: 1800 / 2000 [ 90%] (Sampling)Elapsed Time: 26.472 seconds (Warm-up)

           20.529 seconds (Sampling)
           47.001 seconds (Total)

Iteration: 2000 / 2000 [100%] (Sampling)

Elapsed Time: 27.669 seconds (Warm-up) 21.725 seconds (Sampling) 49.394 seconds (Total)

Iteration: 1800 / 2000 [ 90%] (Sampling) Iteration: 2000 / 2000 [100%] (Sampling)

Elapsed Time: 23.851 seconds (Warm-up) 28.642 seconds (Sampling) 52.493 seconds (Total)

Iteration: 2000 / 2000 [100%] (Sampling)

Elapsed Time: 27.923 seconds (Warm-up) 25.465 seconds (Sampling) 53.388 seconds (Total)

sreedta8 commented 3 years ago

@adamhaber

Installation steps for Windows 10 Anaconda 64-bit (For Linus/Ubuntu 20.04 there are a couple of extra steps - I will post that separately)

  1. Create a new environment: conda create -n randpy python==3.6.10

  2. Activate new environment: activate randpy

  3. In randpy environment install r-base: conda install -c r r-base

  4. In randpy environment install rpy2 with pip: pip install rpy2

  5. In randpy environment install pybrms with pip: pip install pybrms

After r-base(3.6.1), rpy2-3.3.6, and pybrms installation, here is the syntax I followed:

import rpy2.robjects as robjects import rpy2.robjects.packages as rpackages from rpy2.robjects.vectors import StrVector

utils = rpackages.importr("utils") utils.install_packages(StrVector(('rsconnect', 'brms'))) # this installation will take some time

testing the brms and pybrms installation

brms = rpackages.importr("brms") from pybrms import pybrms epilepsy = pybrms.get_brms_data("epilepsy") model = pybrms.fit( formula = "count ~ zAge + zBase x Trt + (1 | patient)", data = epilepsy, family = "poisson" )

sreedta8 commented 3 years ago

@adamhaber On Linux Ubuntu 20.04 I did not succeed with the Poisson modeling process using epilepsy data. See the code below and error generated. I will recreate the environment and reinstall to check what went wrong.

Traceback (most recent call last): File "", line 1, in File "/home/dattazs/miniconda3/envs/randpy/lib/python3.6/site-packages/pybrms/pybrms.py", line 21, in get_brms_data return pd.DataFrame(rpackages.data(brms).fetch(dataset_name)[dataset_name]) NameError: name 'brms' is not defined

I went through so many changes and installs. I'm sure I broke something somewhere.

Sree

sreedta8 commented 3 years ago

@adamhaber I tried a completely new environment from scratch and at the following step:

epilepsy = pybrms.get_brms_data("epilepsy")

Traceback (most recent call last): File "", line 1, in File "/home/sreezach/miniconda3/envs/randpy/lib/python3.6/site-packages/pybrms/pybrms.py", line 21, in get_brms_data return pd.DataFrame(rpackages.data(brms).fetch(dataset_name)[dataset_name]) NameError: name 'brms' is not defined

sreedta8 commented 3 years ago

Hi @adamhaber I once again tried a new environment for brms and pybrms in Ubuntu without success.

This time the installation of python=3.6.10, r-base=3.6.1, rpy2=3.3.4, and pybrms went without a hitch as also the installation of "brms". In other words I could access rpy2 and install "brms" cleanly [I had to install r-curl, r-libcurl, r-v8, and r-libv8 into the environment using Conda and not R=3.6.1]

I ended up getting the following error:

sreedta8 commented 3 years ago

Hi @adamhaber I have tried the following configurations to get pybrms to work with brms, rpy2, and R - No success yet on Linux/Ubuntu. They all fail at the same step: epilepsy = pybrms.get_brms_data("epilepsy") with brms not found - although brms has been installed correctly. I do not see any r-curl / r-v8 errors - those errors have been completely eliminated.

In all of the testing I had done I also applied the solution you linked for non-system R installations: export LD_LIBRARY_PATH="$(python -m rpy2.situation LD_LIBRARY_PATH)":${LD_LIBRARY_PATH}

1: python=3.6.10 (default conda) r-base 3.6.1(default conda) rpy2-3.3.4(default pip) pybrms-0.0.33(default pip)

  1. python=3.8.3 (default conda) r-base 4.0.3(from conda-forge) rpy2-3.3.4(default pip) pybrms-0.0.33(default pip)

  2. python=3.6.10 (from conda-forge) r-base 3.6.1(from conda-forge) rpy2-3.3.4(default pip) pybrms-0.0.33(default pip)

  3. python=3.6.10 (from conda-forge) r-base 3.6.1(from conda-forge) rpy2-3.3.4(from conda-forge) pybrms-0.0.33(default pip)

  4. python=3.6.10 (from conda-forge) r-base 3.6.1(from conda-forge) rpy2-3.3.4(from conda-forge) pybrms-0.0.33(default pip) [In this configuration I also installed all brms related binaries directly from conda-forge (using pre-compiled binaries) and it still did not work

  5. python=3.6.10 (from conda-forge) r-base 3.5.3(from conda-forge) rpy2-3.3.4(default pip) pybrms-0.0.33(default pip)

  6. python=3.6.10 (from conda-forge) r-base 3.4.3(from conda-forge) rpy2-3.3.4(default pip) pybrms-0.0.33(default pip)

I will continue to try and see if I can find the correct setup.

sreedta8 commented 3 years ago

Here is a configuration on Ubuntu that almost succeeded (see the Conda list output below). I can get python, r-base, and rpy2 to communicate with each other,but fails with pybrms (at the epilepsy data step - see below)

It allows me to use rpy2 and install brms from Cloud R repository but fails with the statement when extracting epilepsy data:

epilepsy = pybrms.get_brms_data("epilepsy")
Traceback (most recent call last): File "", line 1, in File "/home/sreezach/miniconda3/envs/asd/lib/python3.6/site-packages/pybrms/pybrms.py", line 21, in get_brms_data return pd.DataFrame(rpackages.data(brms).fetch(dataset_name)[dataset_name]) NameError: name 'brms' is not defined

It uses: NAME VERSION BUILD CHANNEL

  1. python 3.6.11 h425cb1d_0_cpython conda-forge
  2. r-base 4.0.3 hd272fe0_2 conda-forge
  3. rpy2 3.3.6 pypi_0 pypi
  4. pybrms 0.0.33 pypi_0 pypi

In this configuration,

  1. I first installed V8 using conda install -c conda-forge r-v8 (this seems to create the right curl and v8 libraries)
  2. Then I installed r-brms using conda install -c conda-forge r-brms (this results in all the dependencies being installed)

It would be great if you could try to trouble shoot this if you have any time. I will report on any further progress

Complete package / library list Name Version Build Channel _libgcc_mutex 0.1 main
_r-mutex 1.0.0 anacondar_1
attrs 20.2.0 pypi_0 pypi binutils_impl_linux-64 2.33.1 he6710b0_7
binutils_linux-64 2.33.1 h9595d00_15
bwidget 1.9.11 1
bzip2 1.0.8 h7b6447c_0
c-ares 1.16.1 h516909a_3 conda-forge ca-certificates 2020.6.20 hecda079_0 conda-forge cairo 1.16.0 h488836b_1006 conda-forge certifi 2020.6.20 py36h9880bd3_2 conda-forge cffi 1.14.3 pypi_0 pypi curl 7.71.1 he644dc0_8 conda-forge cython 0.29.21 pypi_0 pypi fontconfig 2.13.1 h1056068_1002 conda-forge freetype 2.10.3 h5ab3b9f_0
fribidi 1.0.10 h7b6447c_0
gcc_impl_linux-64 7.3.0 habb00fd_1
gcc_linux-64 7.3.0 h553295d_15
gettext 0.19.8.1 hf34092f_1003 conda-forge gfortran_impl_linux-64 7.3.0 hdf63c60_1
gfortran_linux-64 7.3.0 h553295d_15
glib 2.66.1 he1b5a44_1 conda-forge gmp 6.2.0 he1b5a44_3 conda-forge graphite2 1.3.14 h23475e2_0
gsl 2.6 h294904e_0 conda-forge gxx_impl_linux-64 7.3.0 hdf63c60_1
gxx_linux-64 7.3.0 h553295d_15
harfbuzz 2.7.2 hb1ce69c_1 conda-forge icu 67.1 he1b5a44_0 conda-forge importlib-metadata 2.0.0 pypi_0 pypi iniconfig 1.1.1 pypi_0 pypi jinja2 2.11.2 pypi_0 pypi jpeg 9d h516909a_0 conda-forge krb5 1.17.1 hfafb76e_3 conda-forge ld_impl_linux-64 2.33.1 h53a641e_7
libblas 3.8.0 17_openblas conda-forge libcblas 3.8.0 17_openblas conda-forge libcurl 7.71.1 hcdd3856_8 conda-forge libedit 3.1.20191231 h14c3975_1
libev 4.33 h516909a_1 conda-forge libffi 3.2.1 hf484d3e_1007
libgcc-ng 9.1.0 hdf63c60_0
libgfortran-ng 7.5.0 hae1eefd_17 conda-forge libgfortran4 7.5.0 hae1eefd_17 conda-forge libglib 2.66.1 h0dae87d_1 conda-forge libiconv 1.16 h516909a_0 conda-forge liblapack 3.8.0 17_openblas conda-forge libnghttp2 1.41.0 h8cfc5f6_2 conda-forge libopenblas 0.3.10 pthreads_hb3c22a3_4 conda-forge libpng 1.6.37 hbc83047_0
libssh2 1.9.0 h1ba5d50_1
libstdcxx-ng 9.1.0 hdf63c60_0
libtiff 4.1.0 h2733197_1
libuuid 2.32.1 h14c3975_1000 conda-forge libv8 8.4.371.22 h25e1a3d_0 conda-forge libxcb 1.14 h7b6447c_0
libxml2 2.9.10 h68273f3_2 conda-forge lz4-c 1.9.2 heb0550a_3
make 4.2.1 h1bed415_1
markupsafe 1.1.1 pypi_0 pypi ncurses 6.2 he6710b0_1
numpy 1.19.2 pypi_0 pypi openssl 1.1.1h h516909a_0 conda-forge packaging 20.4 pypi_0 pypi pandas 1.1.3 pypi_0 pypi pango 1.42.4 h7062337_4 conda-forge pcre 8.44 he6710b0_0
pcre2 10.35 h2f06484_0 conda-forge pip 20.2.3 py36_0
pixman 0.38.0 h516909a_1003 conda-forge pluggy 0.13.1 pypi_0 pypi py 1.9.0 pypi_0 pypi pybrms 0.0.33 pypi_0 pypi pycparser 2.20 pypi_0 pypi pyparsing 2.4.7 pypi_0 pypi pystan 2.19.1.1 pypi_0 pypi pytest 6.1.1 pypi_0 pypi python 3.6.11 h425cb1d_0_cpython conda-forge python-dateutil 2.8.1 pypi_0 pypi python_abi 3.6 1_cp36m conda-forge pytz 2020.1 pypi_0 pypi r-abind 1.4_5 r40h6115d3f_1003 conda-forge r-askpass 1.1 r40hcdcec82_2 conda-forge r-assertthat 0.2.1 r40h6115d3f_2 conda-forge r-backports 1.1.10 r40hcdcec82_0 conda-forge r-base 4.0.3 hd272fe0_2 conda-forge r-base64enc 0.1_3 r40hcdcec82_1004 conda-forge r-bayesplot 1.7.2 r40h6115d3f_0 conda-forge r-boot 1.3_25 r40h6115d3f_1 conda-forge r-bridgesampling 1.0_0 r40h6115d3f_1 conda-forge r-brms 2.13.5 r40h0357c0b_0 conda-forge r-brobdingnag 1.2_6 r40h6115d3f_1003 conda-forge r-callr 3.5.1 r40h142f84f_0 conda-forge r-checkmate 2.0.0 r40hcdcec82_1 conda-forge r-cli 2.1.0 r40h142f84f_0 conda-forge r-coda 0.19_4 r40h142f84f_0 conda-forge r-codetools 0.2_17 r40h142f84f_0 conda-forge r-colorspace 1.4_1 r40hcdcec82_2 conda-forge r-colourpicker 1.1.0 r40h6115d3f_0 conda-forge r-commonmark 1.7 r40hcdcec82_1002 conda-forge r-crayon 1.3.4 r40h6115d3f_1003 conda-forge r-crosstalk 1.1.0.1 r40h6115d3f_1 conda-forge r-curl 4.3 r40hcdcec82_1 conda-forge r-desc 1.2.0 r40h6115d3f_1003 conda-forge r-digest 0.6.26 r40h1b71b39_0 conda-forge r-dplyr 1.0.2 r40h0357c0b_0 conda-forge r-dt 0.16 r40h142f84f_0 conda-forge r-dygraphs 1.1.1.6 r40h6115d3f_1003 conda-forge r-ellipsis 0.3.1 r40hcdcec82_0 conda-forge r-evaluate 0.14 r40h6115d3f_2 conda-forge r-fansi 0.4.1 r40hcdcec82_1 conda-forge r-farver 2.0.3 r40h0357c0b_1 conda-forge r-fastmap 1.0.1 r40h0357c0b_1 conda-forge r-future 1.19.1 r40h6115d3f_0 conda-forge r-generics 0.0.2 r40h6115d3f_1003 conda-forge r-ggplot2 3.3.2 r40h6115d3f_0 conda-forge r-ggridges 0.5.2 r40h6115d3f_2 conda-forge r-globals 0.13.1 r40h142f84f_0 conda-forge r-glue 1.4.2 r40hcdcec82_0 conda-forge r-gridextra 2.3 r40h6115d3f_1003 conda-forge r-gtable 0.3.0 r40h6115d3f_3 conda-forge r-gtools 3.8.2 r40hcdcec82_1 conda-forge r-htmltools 0.5.0 r40h0357c0b_0 conda-forge r-htmlwidgets 1.5.2 r40h142f84f_0 conda-forge r-httpuv 1.5.4 r40h0357c0b_0 conda-forge r-igraph 1.2.6 r40he5bf551_0 conda-forge r-inline 0.3.16 r40h6115d3f_0 conda-forge r-isoband 0.2.2 r40h0357c0b_0 conda-forge r-jsonlite 1.7.1 r40hcdcec82_0 conda-forge r-labeling 0.3 r40h6115d3f_1003 conda-forge r-later 1.1.0.1 r40h0357c0b_0 conda-forge r-lattice 0.20_41 r40hcdcec82_2 conda-forge r-lazyeval 0.2.2 r40hcdcec82_2 conda-forge r-lifecycle 0.2.0 r40h6115d3f_1 conda-forge r-listenv 0.8.0 r40h6115d3f_1 conda-forge r-lme4 1.1_23 r40h0357c0b_1 conda-forge r-loo 2.3.1 r40h6115d3f_0 conda-forge r-magrittr 1.5 r40h6115d3f_1003 conda-forge r-markdown 1.1 r40hcdcec82_1 conda-forge r-mass 7.3_53 r40hcdcec82_0 conda-forge r-matrix 1.2_18 r40h7fa42b6_3 conda-forge r-matrixstats 0.57.0 r40hcdcec82_0 conda-forge r-mgcv 1.8_33 r40h7fa42b6_0 conda-forge r-mime 0.9 r40hcdcec82_1 conda-forge r-miniui 0.1.1.1 r40h6115d3f_1002 conda-forge r-minqa 1.2.4 r40h6786f55_1005 conda-forge r-munsell 0.5.0 r40h6115d3f_1003 conda-forge r-mvtnorm 1.1_1 r40h9bbef5b_0 conda-forge r-nleqslv 3.3.2 r40h9bbef5b_1005 conda-forge r-nlme 3.1_149 r40h9bbef5b_0 conda-forge r-nloptr 1.2.2.2 r40h0357c0b_0 conda-forge r-openssl 1.4.3 r40he5c4762_0 conda-forge r-packrat 0.5.0 r40h6115d3f_1002 conda-forge r-pillar 1.4.6 r40h6115d3f_0 conda-forge r-pkgbuild 1.1.0 r40h6115d3f_0 conda-forge r-pkgconfig 2.0.3 r40h6115d3f_1 conda-forge r-pkgload 1.1.0 r40h0357c0b_0 conda-forge r-plyr 1.8.6 r40h0357c0b_1 conda-forge r-praise 1.0.0 r40h6115d3f_1004 conda-forge r-prettyunits 1.1.1 r40h6115d3f_1 conda-forge r-processx 3.4.4 r40hcdcec82_0 conda-forge r-promises 1.1.1 r40h0357c0b_0 conda-forge r-ps 1.4.0 r40h0eb13af_0 conda-forge r-purrr 0.3.4 r40hcdcec82_1 conda-forge r-r6 2.4.1 r40h6115d3f_1 conda-forge r-rcolorbrewer 1.1_2 r40h6115d3f_1003 conda-forge r-rcpp 1.0.4.6 r40h0357c0b_1 conda-forge r-rcppeigen 0.3.3.7.0 r40h51c796c_2 conda-forge r-rcppparallel 5.0.2 r40h0357c0b_0 conda-forge r-reshape2 1.4.4 r40h0357c0b_1 conda-forge r-rlang 0.4.7 r40hcdcec82_0 conda-forge r-rprojroot 1.3_2 r40h6115d3f_1003 conda-forge r-rsconnect 0.8.16 r40h6115d3f_1 conda-forge r-rstan 2.21.2 r40h1b71b39_1 conda-forge r-rstantools 2.1.1 r40h0357c0b_0 conda-forge r-rstudioapi 0.11 r40h6115d3f_1 conda-forge r-scales 1.1.1 r40h6115d3f_0 conda-forge r-shiny 1.5.0 r40h6115d3f_0 conda-forge r-shinyjs 2.0.0 r40h6115d3f_0 conda-forge r-shinystan 2.5.0 r40h6115d3f_1003 conda-forge r-shinythemes 1.1.2 r40h6115d3f_2 conda-forge r-sourcetools 0.1.7 r40he1b5a44_1002 conda-forge r-stanheaders 2.21.0_6 r40h6786f55_0 conda-forge r-statmod 1.4.35 r40h6e990d7_0 conda-forge r-stringi 1.5.3 r40h604b29c_0 conda-forge r-stringr 1.4.0 r40h6115d3f_2 conda-forge r-sys 3.4 r40hcdcec82_0 conda-forge r-testthat 2.3.2 r40h0357c0b_1 conda-forge r-threejs 0.3.3 r40h6115d3f_1 conda-forge r-tibble 3.0.4 r40h0eb13af_0 conda-forge r-tidyselect 1.1.0 r40h6115d3f_0 conda-forge r-utf8 1.1.4 r40hcdcec82_1003 conda-forge r-v8 3.2.0 r40h391c6b5_1 conda-forge r-vctrs 0.3.4 r40hcdcec82_0 conda-forge r-viridislite 0.3.0 r40h6115d3f_1003 conda-forge r-withr 2.3.0 r40h6115d3f_0 conda-forge r-xfun 0.18 r40hcdcec82_0 conda-forge r-xtable 1.8_4 r40h6115d3f_3 conda-forge r-xts 0.12.1 r40hcdcec82_0 conda-forge r-yaml 2.2.1 r40hcdcec82_1 conda-forge r-zeallot 0.1.0 r40h6115d3f_1002 conda-forge r-zoo 1.8_8 r40hcdcec82_0 conda-forge readline 8.0 he28a2e2_2 conda-forge rpy2 3.3.6 pypi_0 pypi sed 4.8 hbfbb72e_0 conda-forge setuptools 50.3.0 py36hb0f4dca_1
six 1.15.0 pypi_0 pypi sqlite 3.33.0 h62c20be_0
tk 8.6.10 hbc83047_0
tktable 2.10 h14c3975_0
toml 0.10.1 pypi_0 pypi tzlocal 2.1 pypi_0 pypi wheel 0.35.1 py_0
xorg-kbproto 1.0.7 h14c3975_1002 conda-forge xorg-libice 1.0.10 h516909a_0 conda-forge xorg-libsm 1.2.3 h84519dc_1000 conda-forge xorg-libx11 1.6.12 h516909a_0 conda-forge xorg-libxext 1.3.4 h516909a_0 conda-forge xorg-libxrender 0.9.10 h516909a_1002 conda-forge xorg-renderproto 0.11.1 h14c3975_1002 conda-forge xorg-xextproto 7.3.0 h14c3975_1002 conda-forge xorg-xproto 7.0.31 h14c3975_1007 conda-forge xz 5.2.5 h7b6447c_0
zipp 3.3.1 pypi_0 pypi zlib 1.2.11 h7b6447c_3
zstd 1.4.5 h9ceee32_0

sreedta8 commented 3 years ago

Hi Adam @adamhaber

I was able to get brms working using rpy2 but not with pybrms. It still fails at

epilepsy = pybrms.get_brms_data("epilepsy") ( I want to explore further why the failure is happening )

with the following error

Traceback (most recent call last): File "", line 1, in File "/home/sreezach/miniconda3/envs/asd/lib/python3.6/site-packages/pybrms/pybrms.py", line 21, in get_brms_data return pd.DataFrame(rpackages.data(brms).fetch(dataset_name)[dataset_name]) NameError: name 'brms' is not defined

Here are my commands and output with rpy2 and brms

import rpy2.robjects as robjects import rpy2.robjects.packages as rpackages from rpy2.robjects.vectors import StrVector

I created a small dataset for testing brms. I read it in using rpy2. It has 29 observations Dependent variable y, continuous predictor x1, a factor predictor x2

bregt = robjects.r('read.csv(file = "~/Documents/Bayes/bregt.csv", sep=",", header = T)')

bregt.head()

R object with classes: ('data.frame',) mapped to: [FloatSexpVector, FloatSexpVector, IntSexpVector] y: <class 'rpy2.rinterface.FloatSexpVector'> <rpy2.rinterface.FloatSexpVector object at 0x7f86df636748> [RTYPES.REALSXP] x1: <class 'rpy2.rinterface.FloatSexpVector'> <rpy2.rinterface.FloatSexpVector object at 0x7f86df636808> [RTYPES.REALSXP] x2: <class 'rpy2.rinterface.IntSexpVector'> <rpy2.rinterface.IntSexpVector object at 0x7f86df6367c8> [RTYPES.INTSXP]

brms = rpackages.importr('brms') Calling brms

model = brms.brm('y ~ x1 + (1|x2)', data = bregt, chains=2, cores=2)

Model runs successfully:

SAMPLING FOR MODEL '995205819d16b843832ed07d522c2a2b' NOW (CHAIN 1). Chain 1: Chain 1: Gradient evaluation took 4.9e-05 seconds Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.49 seconds. Chain 1: Adjust your expectations accordingly! Chain 1: Chain 1: Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)

SAMPLING FOR MODEL '995205819d16b843832ed07d522c2a2b' NOW (CHAIN 2). Chain 2: Chain 2: Gradient evaluation took 3.4e-05 seconds Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.34 seconds. Chain 2: Adjust your expectations accordingly! Chain 2: Chain 2: Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 1: Iteration: 2000 / 2000 [100%] (Sampling) Chain 1: Chain 1: Elapsed Time: 0.475047 seconds (Warm-up) Chain 1: 0.418717 seconds (Sampling) Chain 1: 0.893764 seconds (Total) Chain 1: Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 2: Iteration: 2000 / 2000 [100%] (Sampling) Chain 2: Chain 2: Elapsed Time: 0.50745 seconds (Warm-up) Chain 2: 0.808094 seconds (Sampling) Chain 2: 1.31554 seconds (Total) Chain 2:

then I used print(model) to get the model output

Family: gaussian Links: mu = identity; sigma = identity Formula: y ~ x1 + (1 | x2) Data: structure(list(y = c(3.07, 4.84, 2.51, 3.8, 4.46, (Number of observations: 29) Samples: 2 chains, each with iter = 2000; warmup = 1000; thin = 1; total post-warmup samples = 2000

Group-Level Effects: ~x2 (Number of levels: 2) Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sd(Intercept) 1.56 1.08 0.34 4.44 1.00 369 283

Population-Level Effects: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS Intercept 4.36 1.35 1.73 7.13 1.01 304 164 x1 -0.05 0.34 -0.73 0.66 1.00 805 692

Family Specific Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sigma 0.57 0.09 0.43 0.76 1.00 855 691

Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS and Tail_ESS are effective sample size measures, and Rhat is the potential scale reduction factor on split chains (at convergence, Rhat = 1).

adamhaber commented 3 years ago

Just to make sure I understand, when you posted this:

conda install libpython m2w64-toolchain -c msys2 and got pybrms and brms to work

You got pybrms to work, but in your latest post, pybrms did not work even though brms did?

sreedta8 commented 3 years ago

Hi @adamhaber

The conda install libpython portion was for the Windows 10 Anaconda brms and pybrms setup - That worked out of the box except for the compilation. Anaconda defaults to MS Visual Studio compilers on Windows. I had to install the MSYS2 tool chain for libpythonto ensure brms compiles correctly.

I have been testing brms and pybrms on Linux. That is what I reported about last that I got rpy2-3.3.6 to work with brms but not brms with pybrms.

The test with pybrms and brms always fails at the stage of

epilepsy = pybrms.get_brms_data("epilepsy")

with the error that "brms" is not recognized

meereeum commented 3 years ago

Late to this issue, but you can solve this error by adding brms = rpackages.importr('brms') after the python imports in pybrms.py

(I see this is present in the github code, but was absent in my pip-installed pybrms==0.0.33. the error is raised because the variable brms is never defined)

sreedat commented 2 years ago

@meereeum your solution above to add the small piece of missing code solved the problems I faced when I was recently setting up pybrms, rpy2, r-base, and brms to work together.

I will post the complete set of steps to get this to work below

sreedat commented 2 years ago

Thanks to @ahartikainen @adamhaber and @meereeum I have been able to get a clean setup for pystan and for pybrms (to work with rpy2, r-base, and brms to work together) on Windows 10 and Anaconda 64 bit.

Here are the complete steps:

1. Setup a Working Pystan environment (see the solution originally posted by @ahartikainen Setting up Pystan correctly on Windows. Pystan==2.19.1.1 only works with Python < 3.10

It is assumed that users have a working knowledge of Anaconda to run commands, activate environments and deactivate environments at the command prompt

(base) C:\Users\Sreedat>conda create -n stanpy -c conda-forge 

(base) C:\Users\Sreedat>conda activate stanpy

(stanpy) C:\Users\Sreedat>conda install -c msys2 libpython m2w64-toolchain
*check python version (should be < 3.10). If not < 3.10, run ```conda install python==3.9.7```
(stanpy) C:\Users\Sreedat>g++ -v       
*not part of code - command checks gcc compiler has been correctly installed - output should show gcc-5.3.0*

(stanpy) C:\Users\Sreedat>conda install -c conda-forge pystan==2.19.1.1
*a word of caution: ignore instructions that recommend installing pystan with pip for this version on Windows*
*stick to conda*

(stanpy) C:\Users\Sreedat>conda install -c conda-forge notebook

(stanpy) C:\Users\Sreedat>jupyter-notebook

2. Test Pystan installation via Jupyter Notebook

Use the code shown below to test the Pystan install:

import pystan
import nest_asyncio
nest_asyncio.apply()

schools_code = """
data {
  int<lower=0> J;         // number of schools
  real y[J];              // estimated treatment effects
  real<lower=0> sigma[J]; // standard error of effect estimates
}
parameters {
  real mu;                // population treatment effect
  real<lower=0> tau;      // standard deviation in treatment effects
  vector[J] eta;          // unscaled deviation from mu by school
}
transformed parameters {
  vector[J] theta = mu + tau * eta;        // school treatment effects
}
model {
  target += normal_lpdf(eta | 0, 1);       // prior log-density
  target += normal_lpdf(y | theta, sigma); // log-likelihood
}
"""

schools_data = {"J": 8,
                "y": [28,  8, -3,  7, -1,  1, 18, 12],
                "sigma": [15, 10, 16, 11,  9, 11, 10, 18]}

fit = pystan.stan(model_code=schools_code, data=schools_data)
print(fit)

3. Setting up r-base, rpy2, and pybrms

(stanpy) C:\Users\Sreedat>conda install -c conda-forge r-base==4.1.2

(stanpy) C:\Users\Sreedat>conda install -c conda-forge rpy2==3.4.5

(stanpy) C:\Users\Sreedat>pip install pybrms
(only place to use pip to install)

At this stage, before running anything else, you have to add/create R_HOME variable in User Environment Variable via Conda. Change the location to where you installed Anaconda3. I installed under my user name.

conda env config vars set os.environ['R_HOME']="C:\Users\Sreedat\Anaconda3\envs\stanpy\Lib\R"

(stanpy) C:\Users\Sreedat>echo %R_HOME%

Output should show C:\Users\Sreedat\Anaconda3\envs\stanpy\Lib\R (equivalent location for you) After this step, deactivate and reactivate your stanpy environment

4. Connecting Python (pybrms) with R (brms) via Rpy2

(stanpy) C:\Users\Sreedat>python

should result in

Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Note: Your Python version may be different

>>>import rpy2.robjects as robjects
>>>import rpy2.robjects.packages as rpackages
>>>from rpy2.robjects.vectors import StrVector
>>>utils = rpackages.importr("utils")
>>>utils.install_packages(StrVector(('rsconnect', 'brms')))

All of the 5 commands should run without any error. The last command will install a bunch of packages in R and so will take some time to complete.

At this stage open the file called pybrms.py. The top 15 lines should look like the following:

# AUTOGENERATED! DO NOT EDIT! File to edit: core.ipynb (unless otherwise specified).

__all__ = ['get_brms_data', 'get_stan_code', 'fit']

# Cell
#hide
import typing
import pandas as pd
import numpy as np
import pystan
import re

import rpy2.robjects.packages as rpackages
from rpy2.robjects import default_converter, pandas2ri, numpy2ri, ListVector, DataFrame, StrVector
from rpy2.robjects.conversion import localconverter

Add the following line if it is not present as the text in line 16 (edit using Notepad++, do not use Notepad) brms = rpackages.importr("brms")

Then save the file and close it. Run the next set of commands shown below:

>>>brms = rpackages.importr("brms")
>>>from pybrms import pybrms
>>>epilepsy = pybrms.get_brms_data("epilepsy")

These should run without any error. If any error occurs please check that you have followed all of the above steps. Sometimes errors occur with any updates. Check if the error has any resolution here. And if not, report the error here on ``GitHub/pybrms.

Next run the following command(s):

>>>model = pybrms.fit(
        formula = "count ~ zAge + zBase * Trt + (1 | patient)",
        data = epilepsy, 
        family = "poisson"
        )

You should see an output as follows:

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_b3b8276c0427f26cf5674e1aa3d49273 NOW.

Gradient evaluation took 0 seconds
1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Adjust your expectations accordingly!

Iteration:    1 / 2000 [  0%]  (Warmup)

Gradient evaluation took 0 seconds
1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Adjust your expectations accordingly!

Iteration:    1 / 2000 [  0%]  (Warmup)

Gradient evaluation took 0 seconds
1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Adjust your expectations accordingly!

Iteration:    1 / 2000 [  0%]  (Warmup)

Gradient evaluation took 0.001 seconds
1000 transitions using 10 leapfrog steps per transition would take 10 seconds.
Adjust your expectations accordingly!

Iteration:    1 / 2000 [  0%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  600 / 2000 [ 30%]  (Warmup)
Iteration:  600 / 2000 [ 30%]  (Warmup)
Iteration:  600 / 2000 [ 30%]  (Warmup)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration:  600 / 2000 [ 30%]  (Warmup)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration: 1200 / 2000 [ 60%]  (Sampling)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration: 1200 / 2000 [ 60%]  (Sampling)
Iteration: 1400 / 2000 [ 70%]  (Sampling)
Iteration: 1200 / 2000 [ 60%]  (Sampling)
Iteration: 1200 / 2000 [ 60%]  (Sampling)
Iteration: 1600 / 2000 [ 80%]  (Sampling)
Iteration: 1400 / 2000 [ 70%]  (Sampling)
Iteration: 1400 / 2000 [ 70%]  (Sampling)
Iteration: 1400 / 2000 [ 70%]  (Sampling)
Iteration: 1800 / 2000 [ 90%]  (Sampling)
Iteration: 1600 / 2000 [ 80%]  (Sampling)
Iteration: 1600 / 2000 [ 80%]  (Sampling)
Iteration: 1600 / 2000 [ 80%]  (Sampling)
Iteration: 2000 / 2000 [100%]  (Sampling)

 Elapsed Time: 2.46 seconds (Warm-up)
               1.992 seconds (Sampling)
               4.452 seconds (Total)

Iteration: 1800 / 2000 [ 90%]  (Sampling)
Iteration: 1800 / 2000 [ 90%]  (Sampling)
Iteration: 1800 / 2000 [ 90%]  (Sampling)
Iteration: 2000 / 2000 [100%]  (Sampling)

 Elapsed Time: 2.886 seconds (Warm-up)
               1.995 seconds (Sampling)
               4.881 seconds (Total)

Iteration: 2000 / 2000 [100%]  (Sampling)

 Elapsed Time: 2.775 seconds (Warm-up)
               2.226 seconds (Sampling)
               5.001 seconds (Total)

Iteration: 2000 / 2000 [100%]  (Sampling)

 Elapsed Time: 2.974 seconds (Warm-up)
               2.045 seconds (Sampling)
               5.019 seconds (Total)

Next run the following command to get the model fit summary:

>>>print(model)

Model output should look as follows:

Inference for Stan model: anon_model_b3b8276c0427f26cf5674e1aa3d49273.
4 chains, each with iter=2000; warmup=1000; thin=1; 
post-warmup draws per chain=1000, total post-warmup draws=4000.

              mean se_mean     sd   2.5%    25%    50%     75%  97.5%  n_eff   Rhat
b[1]           0.1  3.3e-3   0.09  -0.08   0.04    0.1    0.15   0.27    707   1.01
b[2]          0.71  4.0e-3   0.12   0.48   0.62    0.7    0.78   0.96    959    1.0
b[3]         -0.26  6.5e-3   0.17  -0.59  -0.37  -0.26   -0.14   0.08    689    1.0
b[4]          0.05  5.2e-3   0.16  -0.27  -0.06   0.05    0.16   0.38   1006    1.0
Intercept     1.63  3.0e-3   0.08   1.46   1.58   1.63    1.69   1.79    781    1.0
sd_1[1]       0.59  2.4e-3   0.07   0.46   0.54   0.58    0.63   0.75    868   1.01
z_1[1,1]     -0.07    0.01   0.48  -1.04  -0.39  -0.06    0.27   0.84   1937    1.0
z_1[1,2]      1.41  9.9e-3    0.4   0.65   1.14   1.41    1.68   2.22   1616    1.0
z_1[1,3]       0.6    0.01   0.36  -0.11   0.36   0.59    0.83   1.31   1103    1.0
z_1[1,4]       0.4  8.9e-3   0.38  -0.35   0.14   0.41    0.65   1.12   1784    1.0
z_1[1,5]      0.04    0.01   0.46   -0.9  -0.27   0.04    0.34   0.92   1954    1.0
z_1[1,6]      0.27  9.9e-3   0.36  -0.45   0.02   0.28    0.53   0.96   1343    1.0
z_1[1,7]     -0.86    0.02   0.51  -1.88  -1.21  -0.84    -0.5   0.14   1090    1.0
z_1[1,8]     -1.27    0.01   0.46   -2.2  -1.57  -1.26   -0.96  -0.39   1998    1.0
z_1[1,9]     -1.27    0.01   0.56   -2.4  -1.64  -1.25   -0.88  -0.21   3114    1.0
z_1[1,10]    -0.82    0.02   0.65  -2.12  -1.25  -0.81   -0.37   0.41   1078    1.0
z_1[1,11]    -0.26  9.7e-3   0.46  -1.21  -0.57  -0.23    0.06   0.61   2296    1.0
z_1[1,12]    -0.05    0.01   0.47  -0.99  -0.37  -0.04    0.28   0.87   1777    1.0
z_1[1,13]     0.01  9.8e-3   0.46  -0.95  -0.29   0.03    0.33   0.87   2246    1.0
z_1[1,14]    -0.07    0.01   0.47  -1.05  -0.38  -0.05    0.25   0.81   1973    1.0
z_1[1,15]     0.23    0.01    0.5  -0.77   -0.1   0.23    0.56   1.16   1882    1.0
z_1[1,16]    -0.42    0.01   0.47  -1.39  -0.73  -0.41   -0.11   0.48   1980    1.0
z_1[1,17]     0.58  9.3e-3   0.38  -0.17   0.32   0.58    0.84    1.3   1661    1.0
z_1[1,18]     1.95    0.01   0.37   1.22   1.71   1.95     2.2    2.7    931   1.01
z_1[1,19]     -1.2    0.01    0.6  -2.43  -1.59  -1.18   -0.79  -0.09   1838    1.0
z_1[1,20]    -0.24    0.01   0.55  -1.37   -0.6  -0.23    0.12   0.83   2214    1.0
z_1[1,21]      0.8    0.01   0.37    0.1   0.55    0.8    1.06   1.52   1100   1.01
z_1[1,22]    -0.43    0.01   0.44  -1.32  -0.72  -0.43   -0.14   0.45   1344    1.0
z_1[1,23]     -0.1    0.01   0.52  -1.13  -0.45  -0.09    0.25   0.89   2288    1.0
z_1[1,24]     0.27    0.01   0.39   -0.5   0.02   0.27    0.52   1.03   1398    1.0
z_1[1,25]    -0.63    0.01   0.57  -1.78  -1.01  -0.62   -0.24   0.45   2910    1.0
z_1[1,26]     0.29  9.6e-3   0.49  -0.69  -0.03    0.3    0.62   1.22   2556    1.0
z_1[1,27]     0.79    0.01   0.47   -0.1   0.47   0.77     1.1   1.72   1482    1.0
z_1[1,28]    -0.35    0.01    0.5   -1.4  -0.67  -0.33 -8.6e-3    0.6   2425    1.0
z_1[1,29]     2.29    0.01   0.37   1.59   2.05   2.29    2.54   3.04    818    1.0
z_1[1,30]     0.69    0.01   0.45  -0.22   0.39   0.69    0.99   1.56   1673    1.0
z_1[1,31]    -0.04  9.9e-3   0.53  -1.09   -0.4  -0.04    0.32   0.97   2858    1.0
z_1[1,32]    -0.95    0.01   0.44  -1.83  -1.24  -0.94   -0.65  -0.12   1610    1.0
z_1[1,33]     0.43  9.7e-3   0.38  -0.33   0.17   0.43    0.69   1.19   1571    1.0
z_1[1,34]    -0.18    0.01   0.52  -1.23  -0.53  -0.16    0.18   0.81   1573    1.0
z_1[1,35]    -0.94    0.01   0.63  -2.25  -1.35   -0.9   -0.51   0.26   3716    1.0
z_1[1,36]    -0.97    0.01   0.59  -2.24  -1.35  -0.94   -0.56   0.11   3193    1.0
z_1[1,37]    -0.16    0.01   0.53  -1.22  -0.52  -0.15    0.19   0.87   1731    1.0
z_1[1,38]     1.29    0.01   0.35   0.61   1.05   1.29    1.53   1.99    834    1.0
z_1[1,39]      0.5    0.01   0.41  -0.33   0.23    0.5    0.77    1.3   1652    1.0
z_1[1,40]     0.74    0.01   0.38  -0.02   0.49   0.74    0.99    1.5   1061    1.0
z_1[1,41]    -0.33    0.01   0.56  -1.49  -0.68   -0.3    0.06   0.71   2993    1.0
z_1[1,42]     0.72  9.1e-3   0.36   0.02   0.48   0.72    0.97   1.43   1588    1.0
z_1[1,43]    -1.22    0.01   0.63  -2.54  -1.63  -1.21   -0.78  -0.04   3577    1.0
z_1[1,44]     -0.8    0.02   0.83  -2.45  -1.34   -0.8   -0.27   0.87   1322    1.0
z_1[1,45]     0.04    0.01   0.42   -0.8  -0.24   0.04    0.33   0.86   1057   1.01
z_1[1,46]    -0.23  9.6e-3   0.48   -1.2  -0.53  -0.21    0.11   0.66   2492    1.0
z_1[1,47]     0.19  8.8e-3   0.38  -0.55  -0.07    0.2    0.45   0.92   1852    1.0
z_1[1,48]    -1.02    0.01    0.5  -2.08  -1.35  -0.99   -0.66  -0.11   2213    1.0
z_1[1,49]     1.26    0.01   0.36   0.53   1.02   1.26     1.5   1.96    967   1.01
z_1[1,50]    -0.49    0.01   0.52  -1.59  -0.83  -0.47   -0.14   0.48   1377    1.0
z_1[1,51]     0.23  9.6e-3   0.47  -0.72  -0.08   0.25    0.56   1.12   2380    1.0
z_1[1,52]     2.15    0.01   0.39   1.44   1.89   2.15    2.41   2.93   1020    1.0
z_1[1,53]    -1.03  9.9e-3   0.57  -2.21  -1.41   -1.0   -0.63   0.03   3339    1.0
z_1[1,54]    -2.16    0.01   0.67   -3.6  -2.61  -2.14   -1.72   -0.9   3421    1.0
z_1[1,55]     -0.3    0.01   0.53  -1.38  -0.65  -0.28    0.06    0.7   2461    1.0
z_1[1,56]     0.04  8.5e-3    0.4  -0.76  -0.23   0.04    0.32    0.8   2216    1.0
z_1[1,57]    -0.32    0.01   0.51  -1.36  -0.66   -0.3    0.02   0.61   2123    1.0
z_1[1,58]     1.03    0.01   0.43    0.2   0.74   1.03    1.33   1.88    897    1.0
z_1[1,59]   5.2e-3    0.01   0.43  -0.85  -0.28 6.1e-3    0.31   0.82   1426    1.0
r_1_1[1]     -0.04  6.4e-3   0.28  -0.59  -0.23  -0.03    0.15    0.5   1913    1.0
r_1_1[2]      0.82  5.8e-3   0.23   0.38   0.67   0.82    0.97   1.25   1526    1.0
r_1_1[3]      0.35  6.1e-3   0.21  -0.06   0.21   0.35    0.48   0.76   1155    1.0
r_1_1[4]      0.23  5.2e-3   0.22   -0.2   0.08   0.23    0.37   0.67   1771    1.0
r_1_1[5]      0.02  6.0e-3   0.27  -0.52  -0.15   0.03     0.2   0.54   1943    1.0
r_1_1[6]      0.16  5.8e-3   0.21  -0.27   0.01   0.16     0.3   0.57   1341    1.0
r_1_1[7]     -0.49  9.4e-3    0.3  -1.08  -0.69  -0.49   -0.29   0.09   1003    1.0
r_1_1[8]     -0.73  5.6e-3   0.26  -1.25  -0.91  -0.73   -0.57  -0.22   2120    1.0
r_1_1[9]     -0.74  5.5e-3   0.33  -1.41  -0.95  -0.73   -0.51  -0.12   3479    1.0
r_1_1[10]    -0.48    0.01   0.38  -1.26  -0.73  -0.47   -0.21   0.26   1007    1.0
r_1_1[11]    -0.15  5.6e-3   0.27  -0.69  -0.33  -0.13    0.03   0.36   2316    1.0
r_1_1[12]    -0.03  6.3e-3   0.27  -0.55  -0.21  -0.02    0.16   0.49   1863    1.0
r_1_1[13]   8.5e-3  5.8e-3   0.27  -0.54  -0.17   0.02    0.19   0.51   2146    1.0
r_1_1[14]    -0.04  6.1e-3   0.27   -0.6  -0.22  -0.03    0.15   0.48   1990    1.0
r_1_1[15]     0.13  6.9e-3   0.29  -0.45  -0.06   0.14    0.33   0.69   1792    1.0
r_1_1[16]    -0.24  5.9e-3   0.27  -0.79  -0.42  -0.24   -0.06   0.28   2098    1.0
r_1_1[17]     0.34  5.4e-3   0.22   -0.1   0.19   0.34    0.49   0.76   1605    1.0
r_1_1[18]     1.13  5.4e-3   0.18   0.78   1.01   1.13    1.24   1.49   1085    1.0
r_1_1[19]     -0.7  7.8e-3   0.35  -1.44  -0.92  -0.68   -0.46  -0.05   1979    1.0
r_1_1[20]    -0.14  6.7e-3   0.32  -0.79  -0.35  -0.13    0.07   0.47   2254    1.0
r_1_1[21]     0.46  6.3e-3   0.21   0.06   0.33   0.46    0.61   0.88   1129    1.0
r_1_1[22]    -0.25  7.1e-3   0.26  -0.77  -0.42  -0.25   -0.08   0.27   1311    1.0
r_1_1[23]    -0.06  6.3e-3    0.3  -0.66  -0.26  -0.05    0.15   0.53   2292    1.0
r_1_1[24]     0.16  6.0e-3   0.22  -0.29   0.01   0.16    0.31   0.59   1376    1.0
r_1_1[25]    -0.37  6.2e-3   0.33  -1.03  -0.58  -0.36   -0.14   0.27   2802    1.0
r_1_1[26]     0.17  5.5e-3   0.28  -0.39  -0.02   0.17    0.36   0.71   2589    1.0
r_1_1[27]     0.46  7.1e-3   0.28  -0.06   0.27   0.45    0.63   1.01   1500    1.0
r_1_1[28]     -0.2  5.9e-3   0.29   -0.8  -0.39   -0.2 -5.6e-3   0.34   2377    1.0
r_1_1[29]     1.33  5.1e-3   0.17   0.99   1.22   1.33    1.43   1.66   1077    1.0
r_1_1[30]      0.4  6.3e-3   0.26  -0.12   0.23    0.4    0.58    0.9   1697    1.0
r_1_1[31]    -0.02  5.9e-3   0.31  -0.64  -0.23  -0.02    0.19   0.56   2758    1.0
r_1_1[32]    -0.55  6.3e-3   0.25  -1.05  -0.71  -0.55   -0.38  -0.07   1558    1.0
r_1_1[33]     0.25  5.6e-3   0.22   -0.2    0.1   0.25     0.4   0.69   1608    1.0
r_1_1[34]     -0.1  7.5e-3    0.3  -0.72  -0.31   -0.1    0.11   0.47   1604    1.0
r_1_1[35]    -0.54  6.6e-3   0.37  -1.33  -0.79  -0.52    -0.3   0.15   3114    1.0
r_1_1[36]    -0.56  6.2e-3   0.34  -1.28  -0.79  -0.55   -0.33   0.07   3065    1.0
r_1_1[37]    -0.09  7.4e-3   0.31   -0.7   -0.3  -0.08    0.11   0.51   1706    1.0
r_1_1[38]     0.75  6.4e-3   0.19   0.37   0.61   0.74    0.88   1.14    934    1.0
r_1_1[39]     0.29  5.8e-3   0.24  -0.18   0.13   0.29    0.45   0.75   1665    1.0
r_1_1[40]     0.43  6.6e-3   0.22  -0.01   0.29   0.43    0.58   0.85   1083    1.0
r_1_1[41]    -0.19  6.1e-3   0.33  -0.86  -0.39  -0.18    0.03   0.42   2849    1.0
r_1_1[42]     0.42  5.0e-3   0.21   0.01   0.28   0.42    0.55   0.83   1696    1.0
r_1_1[43]    -0.71  6.4e-3   0.37  -1.51  -0.94   -0.7   -0.45  -0.02   3399    1.0
r_1_1[44]    -0.46    0.01   0.49  -1.44  -0.78  -0.46   -0.15   0.51   1286    1.0
r_1_1[45]     0.02  7.7e-3   0.25  -0.47  -0.14   0.02    0.19   0.52   1040   1.01
r_1_1[46]    -0.13  5.6e-3   0.28   -0.7  -0.31  -0.12    0.06    0.4   2414    1.0
r_1_1[47]     0.11  5.1e-3   0.22  -0.32  -0.04   0.11    0.26   0.54   1865    1.0
r_1_1[48]    -0.59  6.4e-3   0.29  -1.18  -0.78  -0.58   -0.38  -0.06   2070    1.0
r_1_1[49]     0.73  5.8e-3    0.2   0.32    0.6   0.73    0.86   1.11   1142    1.0
r_1_1[50]    -0.28  8.2e-3    0.3  -0.89  -0.48  -0.27   -0.08   0.28   1335    1.0
r_1_1[51]     0.13  5.5e-3   0.27  -0.41  -0.05   0.14    0.32   0.65   2372    1.0
r_1_1[52]     1.25  5.1e-3   0.19   0.89   1.11   1.24    1.38   1.63   1424    1.0
r_1_1[53]     -0.6  5.7e-3   0.33  -1.27  -0.81  -0.59   -0.37   0.02   3386    1.0
r_1_1[54]    -1.26  7.9e-3   0.41  -2.16  -1.52  -1.25   -0.98  -0.51   2738    1.0
r_1_1[55]    -0.17  6.4e-3    0.3  -0.79  -0.37  -0.16    0.04    0.4   2267    1.0
r_1_1[56]     0.02  5.0e-3   0.23  -0.44  -0.14   0.02    0.18   0.47   2177    1.0
r_1_1[57]    -0.18  5.9e-3   0.29  -0.77  -0.38  -0.18    0.01   0.37   2447    1.0
r_1_1[58]      0.6  8.0e-3   0.24   0.13   0.43    0.6    0.76   1.08    932    1.0
r_1_1[59]   4.1e-3  6.7e-3   0.25   -0.5  -0.16 3.7e-3    0.18   0.49   1405    1.0
b_Intercept   1.76  4.3e-3   0.12   1.52   1.69   1.77    1.84    2.0    780    1.0
lp__        -700.8    0.28   7.86 -717.1 -706.0 -700.6  -695.4 -686.4    770   1.01

Samples were drawn using NUTS at Wed Feb 23 14:09:51 2022.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at 
convergence, Rhat=1).
sreedat commented 2 years ago

@adamhaber Let me know if you want me to create a separate Pybrms installation document. I reiterate what I believed about this library - brms is a great tool and pybrms is wonderful front-end to that tool with the help of rpy2. More users need to be aware and use the library you have created! Thanks once again for your effort!

ahartikainen commented 2 years ago

I tested and following commands where only needed to get a working pybrms (assuming here the latest PR version for cmdstanpy #7 ) on Windows.

conda create -n stan python=3.9 -c conda-forge
conda activate stan
conda install cmdstanpy r-base rpy2 -c conda-forge
conda env config vars set R_HOME="%CONDA_PREFIX%\Lib\R"
conda deactivate
conda activate stan
pip install pybrms

Also, CmdStanPy works also outside conda, so just working R and CmdStanPy are really needed.

sreedat commented 2 years ago

@ahartikainen thanks for the code and directions! I will test it. With the setup you have provided is cmdstan installed and configured by cmdstanpy?

ahartikainen commented 2 years ago

Yes, conda-forge will install cmdstan that is ready to be used.

sreedat commented 2 years ago

@ahartikainen @adamhaber the current version of pybrms references pystan in the pybrms.py file. What is the correct API so that cmdstan can be used? I'm not an experienced programmer so any help would be great

sreedat commented 2 years ago

@ahartikainen when I run pip install pybrms pystan is being installed as a dependency. Would n't that be redundant and/or problematic with cmdstan being installed as a backend?

sreedat commented 2 years ago

@ahartikainen @adamhaber When I run the command from pybrms import pybrms fails with the following error:

>>> from pybrms import pybrms
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Sreedat\.conda\envs\cstan\l
    import pystan
ModuleNotFoundError: No module named 'pystan'