我正在建立带有协变量模型的Weibull AFT,以便使用PyMC3和theano.tensor进行生存分析。我一直在阅读与PyMC3中与生存分析相关的所有笔记本来建立模型。但是,出现以下错误:
*File "C:\Users\71516706\Anaconda3\lib\site-
packages\theano\tensor\basic.py", line 200, in as_tensor_variable
raise AsTensorError("Cannot convert %s to TensorType" % str_x, type(x))
AsTensorError: ('Cannot convert 0 Elemwise{neg,no_inplace}.0\n3
Elemwise{neg,no_inplace}.0\n4 Elemwise{neg,no_inplace}.0\n6
Elemwise{neg,no_inplace}.0\n8 Elemwise{neg,no_inplace}.0\n9
Elemwise{neg,no_inplace}.0\n11 Elemwise{neg,no_inplace}.0\n12
Elemwise{neg,no_inplace}.0\n14 Elemwise{neg,no_inplace}.0\n16
Elemwise{neg,no_inplace}.0\n17 Elemwise{neg,no_inplace}.0\n18
Elemwise{neg,no_inplace}.0\n19 Elemwise{neg,no_inplace}.0\n20
Elemwise{neg,no_inplace}.0\n21 Elemwise{neg,no_inplace}.0\n23
Elemwise{neg,no_inplace}.0\n25 Elemwise{neg,no_inplace}.0\n28
Elemwise{neg,no_inplace}.0\n29 Elemwise{neg,no_inplace}.0\nName:
runningtime_cens, dtype: object to TensorType', <class
'pandas.core.series.Series'>)*
我要编码的模型与生命线(https://lifelines.readthedocs.io/en/latest/lifelines.fitters.html#module-lifelines.fitters.weibull_aft_fitter)中的类weibull_aft_fitter相同
我的代码是:
import pandas as pd
import numpy as np
import pymc3 as pm
import theano.tensor as tt
# Defining Log complementary cdf of Weibull distribution
def weibull_lccdf(x, alpha, beta):
''' Log complementary cdf of Weibull distribution. '''
return -(x / beta)**alpha
# Importing Weibull data
data_weibull=pd.read_csv("data_weibull.csv")
# censored list
event = data_weibull['event'].values.astype(bool)
# Specifying the model
with pm.Model() as model:
# Priors for unknown model parameters
rho = pm.Gamma('rho', alpha=0.001, beta=0.001, testval=2)
beta = pm.Normal('beta', mu=0, sd=0.001, shape=2, testval=0)
# Expected value of lambda parameter
lambda_obs = pm.Deterministic('lambda_obs', \
tt.exp(beta[0]+beta[1]*data_weibull['temperature'][event]))
lambda_cens = pm.Deterministic('lambda_cens', \
tt.exp(beta[0]+beta[1]*data_weibull['temperature'][~event]))
# Likelihood (sampling distribution) of observations
runningtime_obs = pm.Weibull('runningtime_obs', alpha=rho, \
beta=lambda_obs, observed=data_weibull['runningtime'][event])
runningtime_cens = pm.Potential('runningtime_cens', \
weibull_lccdf(data_weibull['runningtime'][~event], rho, \
lambda_cens))
nc=3
ni=10000
nb=1000
nt=5
with model:
# draw ni posterior samples
step = pm.NUTS(vars=[rho, beta])
start = pm.find_MAP()
trace = pm.sample(draws=ni, tune=nb, chains=nc, step=step, cores=1,
start=start)
# Posterior analysis
pm.traceplot(trace, varnames=['rho', 'beta'])
pm.summary(trace, varnames=['rho', 'beta']).round(2)
pm.plot_posterior(trace, varnames=['rho', 'beta'])
答案 0 :(得分:0)
感谢Cam.Davidson.Pilon,我将熊猫对象转换为numpy数组,如下所示:
# Expected value of lambda parameter lambda_obs = pm.Deterministic('lambda_obs', \ tt.exp(beta[0]+beta[1]*data_weibull['temperature'][event].values)) lambda_cens = pm.Deterministic('lambda_cens', \ tt.exp(beta[0]+beta[1]*data_weibull['temperature'][~event].values)) # Likelihood (sampling distribution) of observations runningtime_obs = pm.Weibull('runningtime_obs', alpha=rho, \ beta=lambda_obs, observed=data_weibull['runningtime'][event].values) runningtime_cens = pm.Potential('runningtime_cens', \ weibull_lccdf(data_weibull['runningtime'][~event].values, rho, \ lambda_cens))