python中的分层线性回归错误

问题描述 投票:0回答:1
with pm.Model() as hierarchical_model:
# Hyperpriors for group nodes
mu_a = pm.Normal('mu_a', mu=0., sigma=100)
sigma_a = pm.HalfNormal('sigma_a', 5.)
mu_b = pm.Normal('mu_b', mu=0., sigma=100)
sigma_b = pm.HalfNormal('sigma_b', 5.)
mu_c = pm.Normal('mu_c', mu=0., sigma=100)
sigma_c = pm.HalfNormal('sigma_c', 5.)
mu_d = pm.Normal('mu_d', mu=0., sigma=100)
sigma_d = pm.HalfNormal('sigma_d', 5.)
mu_e = pm.Normal('mu_e', mu=0., sigma=100)
sigma_e = pm.HalfNormal('sigma_e', 5.)
mu_f = pm.Normal('mu_f', mu=0., sigma=100)
sigma_f = pm.HalfNormal('sigma_f', 5.)

# Intercept for each county, distributed around group mean mu_a
# Above we just set mu and sd to a fixed value while here we
# plug in a common group distribution for all a and b (which are
# vectors of length n_counties).
a = pm.Normal('a', mu=mu_a, sigma=sigma_a, shape=n_school)
# Intercept for each county, distributed around group mean mu_a
b = pm.Normal('b', mu=mu_b, sigma=sigma_b, shape=n_school)
c = pm.Normal('c', mu=mu_c, sigma=sigma_c, shape=n_school)
d = pm.Normal('d', mu=mu_d, sigma=sigma_d, shape=n_school)
e = pm.Normal('e', mu=mu_e, sigma=sigma_e, shape=n_school)
f = pm.Normal('f', mu=mu_f, sigma=sigma_f, shape=n_school)

# Model error
eps = pm.HalfCauchy('eps', 5.)

test_score_est = a[school_idx] + b[school_idx]*df.student_ses.values+ c[school_idx]*df.student_iq.values+ d[school_idx]*df.school_avrg._ses.values+ e[school_idx]*df.school_avrg._iq.values+ f[school_idx]*df.student_gender_0.values

# Data likelihood
test_like = pm.Normal('test_like', mu=test_score_est,
                       sigma=eps, observed=df.test_score)

我收到以下错误

OUTPUT: IndexError: index 211 is out of bounds for size 211

**嗨。我收到此错误是为了进行等级制度改革。 n_school = 211为什么我会收到这样的错误?如果您能帮助我,我将不胜感激。预先感谢大家。

下面我提供有关如何计算n_school和school_idx的信息。**

school_idx=df.school_id.values
n_school=len(df.school_id.unique())
OUTPUT= n_school=211
python linear-regression
1个回答
0
投票

Python是0索引的,您在数据结构之类的列表中有211个元素,内容将在索引[0,1,2,..,210]中,因此,如果n_school基于索引,则将其减去1。

© www.soinside.com 2019 - 2024. All rights reserved.