R MSM 包:Q 矩阵对于不同的协变量值是相同的,即使转换率不同
R MSM package: the Q matrix is the same for different covariate values, even though transition rates differ
我正在使用 R 包 MSM
将连续时间马尔可夫模型拟合到面板数据集。因为我对转换率的性别差异感兴趣,所以我用协变量 sex
(“M”或“F”)拟合模型 运行
model_object <- msm(
formula = state ~ nr_years,
subject = id_var,
qmatrix = M, # matrix encoding allowed transitions between states
data = panel_data,
covariates = ~ sex,
control = list(fnscale = 40000, maxit = 1e6) # got these from the help pages
)
拟合模型后,我使用
获得转换率矩阵
qmatrix.msm(model_object, covariates = list(sex = "M"))
qmatrix.msm(model_object, covariates = list(sex = "F"))
这些行是完全相同的转换率矩阵。这有点出乎意料,因为当我使用 hazard.msm
函数提取风险比时,性别之间存在一些差异。有些结果甚至具有统计意义。
我是不是用错了函数?
这可能是因为您的 sex
变量被编码为 character
而不是 factor
。
只需将 sex
转换为 运行 之前的因子,模型就可以解决问题:
panel_data$sex <- factor(panel_data$sex)
这是一个可重现的例子来说明这个棘手的 msm
包行为:
让我们首先在 cav
数据集上创建一个性别变量作为 character
(sex.chr
) 和 factor
(sex.fct
)
library(msm)
library(dplyr)
cav2 <-
cav %>%
mutate(
sex.chr = ifelse(sex == 1, 'M', 'F'),
sex.fct = factor(sex.chr)
)
我们也必须定义初始 qmatrix
:
twoway4.q <- rbind(c(-0.5, 0.25, 0, 0.25), c(0.166, -0.498, 0.166, 0.166),
c(0, 0.25, -0.5, 0.25), c(0, 0, 0, 0))
如果您使用 sex
的 character
版本作为协变量,您将得到两个协变量相同的结果(协变量基本上被忽略)
cav.msm.chr <- msm( state ~ years, subject = PTNUM, data = cav2,
qmatrix = twoway4.q, deathexact = 4, covariates = ~ sex.chr,
control = list ( trace = 2, REPORT = 1 ) )
qmatrix.msm(cav.msm.chr, covariates = list(sex.chr = "M"))
State 1 State 2 State 3
State 1 -0.17747 (-0.19940,-0.15795) 0.13612 ( 0.11789, 0.15718) 0
State 2 0.21992 ( 0.16100, 0.30040) -0.60494 (-0.70972,-0.51563) 0.33409 ( 0.26412, 0.42261)
State 3 0 0.12913 ( 0.07728, 0.21578) -0.41050 (-0.52552,-0.32065)
State 4 0 0 0
State 4
State 1 0.04135 ( 0.03245, 0.05268)
State 2 0.05092 ( 0.01808, 0.14343)
State 3 0.28137 ( 0.21509, 0.36808)
State 4 0
qmatrix.msm(cav.msm.chr, covariates = list(sex.chr = "F"))
State 1 State 2 State 3
State 1 -0.17747 (-0.19940,-0.15795) 0.13612 ( 0.11789, 0.15718) 0
State 2 0.21992 ( 0.16100, 0.30040) -0.60494 (-0.70972,-0.51563) 0.33409 ( 0.26412, 0.42261)
State 3 0 0.12913 ( 0.07728, 0.21578) -0.41050 (-0.52552,-0.32065)
State 4 0 0 0
State 4
State 1 0.04135 ( 0.03245, 0.05268)
State 2 0.05092 ( 0.01808, 0.14343)
State 3 0.28137 ( 0.21509, 0.36808)
State 4 0
但是使用 sex
的阶乘版本会给你预期的结果
cav.msm.fct <- msm( state ~ years, subject = PTNUM, data = cav2,
qmatrix = twoway4.q, deathexact = 4, covariates = ~ sex.fct,
control = list ( trace = 2, REPORT = 1 )
qmatrix.msm(cav.msm.fct, covariates = list(sex.fct = "M"))
State 1 State 2
State 1 -1.234e-01 (-1.750e-01,-8.693e-02) 7.667e-02 ( 4.630e-02, 1.270e-01)
State 2 2.838e-01 ( 1.139e-01, 7.075e-01) -6.435e-01 (-1.115e+00,-3.714e-01)
State 3 0 1.416e-01 ( 1.852e-02, 1.083e+00)
State 4 0 0
State 3 State 4
State 1 0 4.668e-02 ( 2.727e-02, 7.989e-02)
State 2 3.597e-01 ( 1.804e-01, 7.170e-01) 1.938e-05 ( 3.702e-66, 1.014e+56)
State 3 -8.207e-01 (-1.587e+00,-4.244e-01) 6.791e-01 ( 3.487e-01, 1.323e+00)
State 4 0
qmatrix.msm(cav.msm.fct, covariates = list(sex.fct = "F"))
State 1 State 2 State 3
State 1 -0.17747 (-0.19940,-0.15795) 0.13612 ( 0.11789, 0.15718) 0
State 2 0.21992 ( 0.16100, 0.30040) -0.60494 (-0.70972,-0.51563) 0.33409 ( 0.26412, 0.42261)
State 3 0 0.12913 ( 0.07728, 0.21578) -0.41050 #=(-0.52552,-0.32065)
State 4 0 0 0
State 4
State 1 0.04135 ( 0.03245, 0.05268)
State 2 0.05092 ( 0.01808, 0.14343)
State 3 0.28137 ( 0.21509, 0.36808)
State 4 0
```
我正在使用 R 包 MSM
将连续时间马尔可夫模型拟合到面板数据集。因为我对转换率的性别差异感兴趣,所以我用协变量 sex
(“M”或“F”)拟合模型 运行
model_object <- msm(
formula = state ~ nr_years,
subject = id_var,
qmatrix = M, # matrix encoding allowed transitions between states
data = panel_data,
covariates = ~ sex,
control = list(fnscale = 40000, maxit = 1e6) # got these from the help pages
)
拟合模型后,我使用
获得转换率矩阵qmatrix.msm(model_object, covariates = list(sex = "M"))
qmatrix.msm(model_object, covariates = list(sex = "F"))
这些行是完全相同的转换率矩阵。这有点出乎意料,因为当我使用 hazard.msm
函数提取风险比时,性别之间存在一些差异。有些结果甚至具有统计意义。
我是不是用错了函数?
这可能是因为您的 sex
变量被编码为 character
而不是 factor
。
只需将 sex
转换为 运行 之前的因子,模型就可以解决问题:
panel_data$sex <- factor(panel_data$sex)
这是一个可重现的例子来说明这个棘手的 msm
包行为:
让我们首先在 cav
数据集上创建一个性别变量作为 character
(sex.chr
) 和 factor
(sex.fct
)
library(msm)
library(dplyr)
cav2 <-
cav %>%
mutate(
sex.chr = ifelse(sex == 1, 'M', 'F'),
sex.fct = factor(sex.chr)
)
我们也必须定义初始 qmatrix
:
twoway4.q <- rbind(c(-0.5, 0.25, 0, 0.25), c(0.166, -0.498, 0.166, 0.166),
c(0, 0.25, -0.5, 0.25), c(0, 0, 0, 0))
如果您使用 sex
的 character
版本作为协变量,您将得到两个协变量相同的结果(协变量基本上被忽略)
cav.msm.chr <- msm( state ~ years, subject = PTNUM, data = cav2,
qmatrix = twoway4.q, deathexact = 4, covariates = ~ sex.chr,
control = list ( trace = 2, REPORT = 1 ) )
qmatrix.msm(cav.msm.chr, covariates = list(sex.chr = "M"))
State 1 State 2 State 3
State 1 -0.17747 (-0.19940,-0.15795) 0.13612 ( 0.11789, 0.15718) 0
State 2 0.21992 ( 0.16100, 0.30040) -0.60494 (-0.70972,-0.51563) 0.33409 ( 0.26412, 0.42261)
State 3 0 0.12913 ( 0.07728, 0.21578) -0.41050 (-0.52552,-0.32065)
State 4 0 0 0
State 4
State 1 0.04135 ( 0.03245, 0.05268)
State 2 0.05092 ( 0.01808, 0.14343)
State 3 0.28137 ( 0.21509, 0.36808)
State 4 0
qmatrix.msm(cav.msm.chr, covariates = list(sex.chr = "F"))
State 1 State 2 State 3
State 1 -0.17747 (-0.19940,-0.15795) 0.13612 ( 0.11789, 0.15718) 0
State 2 0.21992 ( 0.16100, 0.30040) -0.60494 (-0.70972,-0.51563) 0.33409 ( 0.26412, 0.42261)
State 3 0 0.12913 ( 0.07728, 0.21578) -0.41050 (-0.52552,-0.32065)
State 4 0 0 0
State 4
State 1 0.04135 ( 0.03245, 0.05268)
State 2 0.05092 ( 0.01808, 0.14343)
State 3 0.28137 ( 0.21509, 0.36808)
State 4 0
但是使用 sex
的阶乘版本会给你预期的结果
cav.msm.fct <- msm( state ~ years, subject = PTNUM, data = cav2,
qmatrix = twoway4.q, deathexact = 4, covariates = ~ sex.fct,
control = list ( trace = 2, REPORT = 1 )
qmatrix.msm(cav.msm.fct, covariates = list(sex.fct = "M"))
State 1 State 2
State 1 -1.234e-01 (-1.750e-01,-8.693e-02) 7.667e-02 ( 4.630e-02, 1.270e-01)
State 2 2.838e-01 ( 1.139e-01, 7.075e-01) -6.435e-01 (-1.115e+00,-3.714e-01)
State 3 0 1.416e-01 ( 1.852e-02, 1.083e+00)
State 4 0 0
State 3 State 4
State 1 0 4.668e-02 ( 2.727e-02, 7.989e-02)
State 2 3.597e-01 ( 1.804e-01, 7.170e-01) 1.938e-05 ( 3.702e-66, 1.014e+56)
State 3 -8.207e-01 (-1.587e+00,-4.244e-01) 6.791e-01 ( 3.487e-01, 1.323e+00)
State 4 0
qmatrix.msm(cav.msm.fct, covariates = list(sex.fct = "F"))
State 1 State 2 State 3
State 1 -0.17747 (-0.19940,-0.15795) 0.13612 ( 0.11789, 0.15718) 0
State 2 0.21992 ( 0.16100, 0.30040) -0.60494 (-0.70972,-0.51563) 0.33409 ( 0.26412, 0.42261)
State 3 0 0.12913 ( 0.07728, 0.21578) -0.41050 #=(-0.52552,-0.32065)
State 4 0 0 0
State 4
State 1 0.04135 ( 0.03245, 0.05268)
State 2 0.05092 ( 0.01808, 0.14343)
State 3 0.28137 ( 0.21509, 0.36808)
State 4 0
```