我正试图从一份出版物中实现一个预测模型请参阅此处以供参考
本文指定了从以前的临床研究中得出的预测模型,并提供了每个模型的系数和协方差矩阵。
我非常熟悉将模型与R中的数据进行拟合,但我从未指定过一个。
具体来说,我希望创建模型,这样我就可以利用predict((为不同的患者组生成预测结果,同时考虑模型的可变性。
为了方便起见,我提供了两个模型中的一个,以及相关的系数和协方差矩阵,两者都是类似的形式
# Model 1
# TKV model
#
# delta_TKV = exp(intercept + a x age + b x Ln(TKV_t) + c x female + d x age x Ln(TKV_t)) - 500
# delta_TKV - the change in total kidney volume (TKV) over a period of time in years
# age - age of patient in years
# Ln(TKV_t) - natural log of total kidney volume at time t
# female - boolean value for gender
# age:Ln(TKV_t) - interaction term between age and Ln(TKV)
# Coefficients Estimate SE
# intercept 0.7889 1.1313
# age 0.1107 0.0287
# Ln(TKV) 0.8207 0.1556
# Female -0.0486 0.0266
# Age:Ln(TKV) -0.0160 0.0039
# Covariance intercept age Ln(TKV) Female Age:Ln(TKV)
# intercept 1.279758 -0.031790 -0.175654 -0.001306 0.004362
# age -0.031790 0.00823 0.004361 -0.000016 -0.000113
# Ln(TKV) -0.175651 0.004361 0.024207 -0.000155 -0.000601
# Female -0.001306 -0.000016 0.000155 0.000708 0.000002
# Age:Ln(TKV) 0.004362 -0.000113 -0.000601 0.000002 0.000016
我不知道您是否可以生成一个模型,用于具有自定义系数的predict
。但您可以使用model.frame
或model.matrix
根据您的公式生成设计矩阵,例如
data = data.frame(delta_TKV = 1:3 , TKV_t = 3.5, female = c(T,F,T), age = 40:42 )
model = model.frame(log(delta_TKV + 500) ~ age + log(TKV_t) + female + age:log(TKV_t),
data)
model
#> (Intercept) age log(TKV_t) femaleTRUE age:log(TKV_t)
#> 1 1 40 1.252763 1 50.11052
#> 2 1 41 1.252763 0 51.36328
#> 3 1 42 1.252763 1 52.61604
#> attr(,"assign")
#> [1] 0 1 2 3 4
#> attr(,"contrasts")
#> attr(,"contrasts")$female
#> [1] "contr.treatment"
coefs = c(
intercept = 0.7889 ,
age = 0.1107 ,
`log(TKV)` = 0.8207 ,
female = -0.0486 ,
`Age:log(TKV)` = -0.0160
)
model %*% coefs
#> [,1]
#> 1 5.394674
#> 2 5.533930
#> 3 5.575986
我对公式进行了转换,使其类似于lm
规范,因此响应是y+500的对数,您必须通过相反的操作获得y
,如果您使用lm
,同样适用