R Under development (unstable) (2023-08-17 r84984 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(testthat) > library(causact) WARNING: The 'r-causact' Conda environment does not exist. To use the 'dag_numpyro()' function, you need to set up the 'r-causact' environment. Run install_causact_deps() when ready to set up the 'r-causact' environment. Attaching package: 'causact' The following objects are masked from 'package:stats': binomial, poisson The following objects are masked from 'package:base': beta, gamma > > test_check("causact") ## The below code will return a posterior distribution ## for the given DAG. Use dag_numpyro(mcmc=TRUE) to return a ## data frame of the posterior distribution: reticulate::py_run_string(" import numpy as np import numpyro as npo import numpyro.distributions as dist import pandas as pd import arviz as az from jax import random from numpyro.infer import MCMC, NUTS from jax.numpy import transpose as t from jax.numpy import (exp, log, log1p, expm1, abs, mean, sqrt, sign, round, concatenate, atleast_1d, cos, sin, tan, cosh, sinh, tanh, sum, prod, min, max, cumsum, cumprod ) ## note that above is from JAX numpy package, not numpy. nTrials = np.array(r.gymDF.nTrialCustomers) #DATA x = np.array(r.gymDF.yogaStretch) #DATA k = np.array(r.gymDF.nSigned) #DATA j = pd.factorize(r.gymDF.gymID,use_na_sentinel=True)[0] #DIM j_dim = len(np.unique(j)) #DIM j_crd = pd.factorize(r.gymDF.gymID,use_na_sentinel=True)[1] #DIM def graph2_model(nTrials,x,k,j): ## Define random variables and their relationships mu_alpha = npo.sample('mu_alpha', dist.Normal(-1,1.5)) mu_beta = npo.sample('mu_beta', dist.Normal(0,0.75)) sd_alpha = npo.sample('sd_alpha', dist.Uniform(0,3)) sd_beta = npo.sample('sd_beta', dist.Uniform(0,1.5)) with npo.plate('j_dim',j_dim): alpha = npo.sample('alpha', dist.Normal(mu_alpha,sd_alpha)) beta = npo.sample('beta', dist.Normal(mu_beta,sd_beta)) y = npo.deterministic('y', alpha[j] + beta[j] * x) theta = npo.deterministic('theta', 1 / (1 + exp(-y))) k = npo.sample('k', dist.Binomial(nTrials,theta),obs=k) # computationally get posterior mcmc = MCMC(NUTS(graph2_model), num_warmup = 1000, num_samples = 4000) rng_key = random.PRNGKey(seed = 111) mcmc.run(rng_key,nTrials,x,k,j) drawsDS = az.from_numpyro(mcmc, coords = {'j_dim': j_crd}, dims = {'alpha': ['j_dim'], 'beta': ['j_dim']} ).posterior # prepare xarray dataset for export to R dataframe dimensions_to_keep = ['chain','draw','j_dim'] drawsDS = drawsDS.squeeze(drop = True ).drop_dims([dim for dim in drawsDS.dims if dim not in dimensions_to_keep]) # unstack plate variables to flatten dataframe as needed for j_da in drawsDS['j_dim']: new_varname = f'alpha_{j_da.values}' drawsDS = drawsDS.assign(**{new_varname:drawsDS['alpha'].sel(j_dim = j_da)}) new_varname = f'beta_{j_da.values}' drawsDS = drawsDS.assign(**{new_varname:drawsDS['beta'].sel(j_dim = j_da)}) drawsDS = drawsDS.drop_dims(['j_dim']) drawsDF = drawsDS.squeeze().to_dataframe()" ) ## END PYTHON STRING drawsDF = reticulate::py$drawsDF This function is currently defunct. It has been superseded by dag_numpyro() because of tricky installation issues related to the greta package's use of tensorflow. If the greta package resolves those issues, this function may return, but please use dag_numpyro() as a direct replacement. ## The below code will return a posterior distribution ## for the given DAG. Use dag_numpyro(mcmc=TRUE) to return a ## data frame of the posterior distribution: reticulate::py_run_string(" import numpy as np import numpyro as npo import numpyro.distributions as dist import pandas as pd import arviz as az from jax import random from numpyro.infer import MCMC, NUTS from jax.numpy import transpose as t from jax.numpy import (exp, log, log1p, expm1, abs, mean, sqrt, sign, round, concatenate, atleast_1d, cos, sin, tan, cosh, sinh, tanh, sum, prod, min, max, cumsum, cumprod ) ## note that above is from JAX numpy package, not numpy. x = np.array(r.carModelDF.getCard) #DATA y = pd.factorize(r.carModelDF.carModel,use_na_sentinel=True)[0] #DIM y_dim = len(np.unique(y)) #DIM y_crd = pd.factorize(r.carModelDF.carModel,use_na_sentinel=True)[1] #DIM def graph_model(x,y): ## Define random variables and their relationships with npo.plate('y_dim',y_dim): theta = npo.sample('theta', dist.Uniform(0,1)) x = npo.sample('x', dist.Bernoulli(theta[y]),obs=x) # computationally get posterior mcmc = MCMC(NUTS(graph_model), num_warmup = 1000, num_samples = 4000) rng_key = random.PRNGKey(seed = 111) mcmc.run(rng_key,x,y) drawsDS = az.from_numpyro(mcmc, coords = {'y_dim': y_crd}, dims = {'theta': ['y_dim']} ).posterior # prepare xarray dataset for export to R dataframe dimensions_to_keep = ['chain','draw','y_dim'] drawsDS = drawsDS.squeeze(drop = True ).drop_dims([dim for dim in drawsDS.dims if dim not in dimensions_to_keep]) # unstack plate variables to flatten dataframe as needed for y_da in drawsDS['y_dim']: new_varname = f'theta_{y_da.values}' drawsDS = drawsDS.assign(**{new_varname:drawsDS['theta'].sel(y_dim = y_da)}) drawsDS = drawsDS.drop_dims(['y_dim']) drawsDF = drawsDS.squeeze().to_dataframe()" ) ## END PYTHON STRING drawsDF = reticulate::py$drawsDF ## The below code will return a posterior distribution ## for the given DAG. Use dag_numpyro(mcmc=TRUE) to return a ## data frame of the posterior distribution: reticulate::py_run_string(" import numpy as np import numpyro as npo import numpyro.distributions as dist import pandas as pd import arviz as az from jax import random from numpyro.infer import MCMC, NUTS from jax.numpy import transpose as t from jax.numpy import (exp, log, log1p, expm1, abs, mean, sqrt, sign, round, concatenate, atleast_1d, cos, sin, tan, cosh, sinh, tanh, sum, prod, min, max, cumsum, cumprod ) ## note that above is from JAX numpy package, not numpy. x = np.array(r.attitude.complaints) #DATA y = np.array(r.attitude.rating) #DATA def graph_model(x,y): ## Define random variables and their relationships int = npo.sample('int', dist.Normal(0,10)) coef = npo.sample('coef', dist.Normal(0,10)) sd = npo.sample('sd', dist.TruncatedCauchy(0,3,low=0)) mu = npo.deterministic('mu', int + coef * x) y = npo.sample('y', dist.Normal(mu,sd),obs=y) # computationally get posterior mcmc = MCMC(NUTS(graph_model), num_warmup = 1000, num_samples = 4000) rng_key = random.PRNGKey(seed = 111) mcmc.run(rng_key,x,y) drawsDS = az.from_numpyro(mcmc).posterior # prepare xarray dataset for export to R dataframe dimensions_to_keep = ['chain','draw'] drawsDS = drawsDS.squeeze(drop = True ).drop_dims([dim for dim in drawsDS.dims if dim not in dimensions_to_keep]) # unstack plate variables to flatten dataframe as needed drawsDF = drawsDS.squeeze().to_dataframe()" ) ## END PYTHON STRING drawsDF = reticulate::py$drawsDF ## The below code will return a posterior distribution ## for the given DAG. Use dag_numpyro(mcmc=TRUE) to return a ## data frame of the posterior distribution: reticulate::py_run_string(" import numpy as np import numpyro as npo import numpyro.distributions as dist import pandas as pd import arviz as az from jax import random from numpyro.infer import MCMC, NUTS from jax.numpy import transpose as t from jax.numpy import (exp, log, log1p, expm1, abs, mean, sqrt, sign, round, concatenate, atleast_1d, cos, sin, tan, cosh, sinh, tanh, sum, prod, min, max, cumsum, cumprod ) ## note that above is from JAX numpy package, not numpy. x = np.array(r.trees.Height) #DATA def graph_model(x): ## Define random variables and their relationships nu = npo.sample('nu', dist.Gamma(2,0.1)) mu = npo.sample('mu', dist.Normal(50,24.5)) sigma = npo.sample('sigma', dist.Uniform(0,50)) x = npo.sample('x', dist.StudentT(nu,mu,sigma),obs=x) # computationally get posterior mcmc = MCMC(NUTS(graph_model), num_warmup = 1000, num_samples = 4000) rng_key = random.PRNGKey(seed = 111) mcmc.run(rng_key,x) drawsDS = az.from_numpyro(mcmc).posterior # prepare xarray dataset for export to R dataframe dimensions_to_keep = ['chain','draw'] drawsDS = drawsDS.squeeze(drop = True ).drop_dims([dim for dim in drawsDS.dims if dim not in dimensions_to_keep]) # unstack plate variables to flatten dataframe as needed drawsDF = drawsDS.squeeze().to_dataframe()" ) ## END PYTHON STRING drawsDF = reticulate::py$drawsDF WARNING: The 'r-causact' Conda environment does not exist. To use the 'dag_numpyro()' function, you need to set up the 'r-causact' environment. Run install_causact_deps() when ready to set up the 'r-causact' environment. [ FAIL 0 | WARN 0 | SKIP 0 | PASS 61 ] > > proc.time() user system elapsed 7.29 0.45 18.59