Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
blups = np.array(
[np.random.normal(est, sigma, num_grps) for est, sigma in zip(b, grp_sigmas)]
).T
# Generate data
for grp in range(blups.shape[0]):
# Create a random design matrix per group
if corrs:
x = easy_multivariate_normal(num_obs, num_coef, corrs, mus, sigmas, seed)
else:
x = np.random.normal(mus, sigmas, size=(num_obs, num_coef))
x = np.column_stack([np.ones((num_obs, 1)), x])
# Use blups to generate group data
y = np.dot(x, blups[grp, :]) + np.random.normal(*noise_params, size=num_obs)
if family == "binomial":
y = discrete_inverse_logit(y)
if grp == 0:
x_all, y_all = x, y
else:
y_all = np.append(y_all, y, axis=0)
x_all = np.append(x_all, x, axis=0)
grp_ids = np.array([[elem] * num_obs for elem in range(1, num_grps + 1)]).ravel()
data = pd.DataFrame(
np.column_stack([y_all, x_all[:, 1:], grp_ids]),
columns=["DV"]
+ ["IV" + str(elem + 1) for elem in range(x_all.shape[1] - 1)]
+ ["Group"],
)
blups = pd.DataFrame(
blups,
assert (
isinstance(noise_params, tuple) and len(noise_params) == 2
), "noise_params should be a tuple of (mean,std)"
# Generate random design matrix
if corrs is not None:
X = easy_multivariate_normal(num_obs, num_coef, corrs, mus, sigmas, seed)
else:
X = np.random.normal(mus, sigmas, size=(num_obs, num_coef))
# Add intercept
X = np.column_stack([np.ones((num_obs, 1)), X])
# Generate data
Y = np.dot(X, b) + np.random.normal(*noise_params, size=num_obs)
# Apply transform if not linear model
if family == "binomial":
Y = discrete_inverse_logit(Y)
dat = pd.DataFrame(
np.column_stack([Y, X[:, 1:]]),
columns=["DV"] + ["IV" + str(elem + 1) for elem in range(X.shape[1] - 1)],
)
return dat, b