Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_install():
"""
Quick function to test installation by import a lmm object and fitting a quick model.
"""
try:
from pymer4.models import Lmer
from pymer4.utils import get_resource_path
import os
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
df = pd.read_csv(os.path.join(get_resource_path(), "sample_data.csv"))
model = Lmer("DV ~ IV3 + (1|Group)", data=df)
model.fit(summarize=False)
print("Pymer4 installation working successfully!")
except Exception as e:
print("Error! {}".format(e))
import pandas as pd
import seaborn as sns
from pymer4.models import Lmer
from pymer4.utils import get_resource_path
df = pd.read_csv(os.path.join(get_resource_path(),'sample_data.csv'))
df.head()
print("Hello World!")
#######################################################################
# Estimate a model
# ----------------
#
# Initialize linear model with random intercepts, slopes and their correlation
model = Lmer('DV ~ IV2 + (IV2|Subject)',data=df)
#########################################################################
# Fit it
model.fit()
#######################################################################
# Inspect clusters
# --------------------------
#
# We can look at the 'Subject' level parameters easily
# Each row here is a unique Subject's random intercept and slope
model.fixef.head()
# We can also plot these values with respect to the population parameters
import os
import pandas as pd
import seaborn as sns
from pymer4.models import Lmer
from pymer4.utils import get_resource_path
df = pd.read_csv(os.path.join(get_resource_path(),'sample_data.csv'))
df.head()
#######################################################################
# Estimate a model
# ----------------
#
# Initialize linear model with random intercepts, slopes and their correlation
model = Lmer('DV ~ IV2 + (IV2|Subject)',data=df)
#########################################################################
# Fit it
model.fit()
#######################################################################
# Inspect clusters
# --------------------------
#
# We can look at the 'Subject' level parameters easily
# Each row here is a unique Subject's random intercept and slope
model.fixef.head()
# We can also plot these values with respect to the population parameters
).astype(model_atts['data_atts'][dtype_name])
# Check if the list already exists if so just append to it
if hasattr(model, item_name):
current_items = getattr(model, item_name)
if current_items is not None:
current_items += [df]
setattr(model, item_name, current_items)
else:
setattr(model, item_name, [df])
# Otherwise create it
else:
setattr(model, item_name, [df])
# Add to the list of completed items
completed.extend([item_name, vals_name, idx_name, dtype_name])
# Now deal with model object in R if needed
if isinstance(model, Lmer):
filename = filepath.split('.')[0]
model.model_obj = base.readRDS(f"{filename}.rds")
return model
else:
raise IOError("filepath must end with .h5 or .hdf5")
Function for loading pymer4 models. A file path ending in .h5 or .hdf5 should be provided. For Lmer models an additional filepath.robj should be located in the same directory.
Args:
model (pymer4.models): an instance of a pymer4 model
filepath (str): full filepath string ending with .h5 or .hd5f
"""
if filepath.endswith(".h5") or filepath.endswith('.hdf5'):
if not os.path.exists(filepath):
raise IOError("File not found!")
# Load h5 first
model_atts = dd.io.load(filepath)
# Figure out what kind of model we're dealing with
if model_atts['simple_atts']['model_class'] == 'Lmer':
model = Lmer('', [])
elif model_atts['simple_atts']['model_class'] == 'Lm2':
model = Lm2('', [] , '')
elif model_atts['simple_atts']['model_class'] == 'Lm':
model = Lm('', [])
# Set top level attributes
for k, v in model_atts['simple_atts'].items():
if k != 'model_class':
setattr(model, k, v)
# Make sure the model formula is a python string string so that rpy2 doesn't complain
model.formula = str(model.formula)
# Set data attributes
# Container for already set items
completed = []
for k, v in model_atts['data_atts'].items():