介绍
glmnet提供了LASSO或ridge regression的Cox-PH分析模式,用于研究预测变量与生存时间的关系。具体做法先训练模型再使用最佳参数构建模型,最后评估预测精确度 C-index值判断预测的优劣。
加载数据
1 | library(glmnet) |
1 | time status |
1 | prof <- x |
1 | Feature1 Feature2 Feature3 Feature4 Feature5 Feature6 Feature7 Feature8 Feature9 Feature10 |
训练参数
设置alpha=0是ridge regression; alpha=1是LASSO;设置lambda,nlambda = 100或者lambda = 10^seq(3, -2, by = -.1);family选择不同数据分布情况,cox是cox-ph(其他选择”gaussian”, “binomial”, “poisson”, “multinomial”, “mgaussian”符合不同的数据)。1
2
3
4
5
6
7
8set.seed(123)
cv.fit <- cv.glmnet(prof, phen,
family = "cox",
type.measure = "C",
nfolds = 10,
alpha = 0,
nlambda = 100)
plot(cv.fit)
y轴坐标C-index原名是Harrell’concordance index,是用于评估模型的预测精度,常用于临床研究。x轴是lambda的log化结果,我们常选择最小的lambda值作为建模参数,也即是途中最大的C-index值。
C-index的计算方法是把所研究的资料中的所有研究对象随机地两两组成对子,以生存分析为例,两个病人如果生存时间较长的一位其预测生存时间长于另一位,或预测的生存概率高的一位的生存时间长于另一位,则称之为预测结果与实际结果相符,称之为一致。
计算C-index=K/M。
从上述计算方法可以看出C-index在0.5-1之间(任意配对随机情况下一致与不一致刚好是0.5的概率)。0.5为完全不一致,说明该模型没有预测作用,1为完全一致,说明该模型预测结果与实际完全一致。一般情况下C-index在0.50-0.70为准确度较低:在0.71-0.90之间为准确度中等;而高于0.90则为高准确度,跟相关系数有点类似。
构建模型
选择cv.fit最小lambda值1
2
3
4
5fit <- glmnet(prof, phen,
family = "cox",
alpha = 0,
lambda = cv.fit$lambda.min)
summary(fit)1
2
3
4
5
6
7
8
9
10
11
12
13 Length Class Mode
a0 0 -none- NULL
beta 30 dgCMatrix S4
df 1 -none- numeric
dim 2 -none- numeric
lambda 1 -none- numeric
dev.ratio 1 -none- numeric
nulldev 1 -none- numeric
npasses 1 -none- numeric
jerr 1 -none- numeric
offset 1 -none- logical
call 6 -none- call
nobs 1 -none- numeric
- 每个features的coefficient
1
coef(fit)
可根据系数选择重要的features进行后续Cox-PH分析。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
3230 x 1 sparse Matrix of class "dgCMatrix"
s0
Feature1 0.5114403878
Feature2 -0.1954830557
Feature3 -0.2405974927
Feature4 0.1957977902
Feature5 -0.2074132864
Feature6 -0.5056236002
Feature7 0.3552934341
Feature8 0.1057532384
Feature9 0.4648827155
Feature10 0.1375101610
Feature11 -0.0194344395
Feature12 0.0047078816
Feature13 0.0410461245
Feature14 0.0021407848
Feature15 -0.0009016771
Feature16 0.0001994629
Feature17 -0.0372298071
Feature18 -0.0100297637
Feature19 0.0080887542
Feature20 -0.0001315014
Feature21 -0.0218432109
Feature22 -0.0238062971
Feature23 -0.0054209272
Feature24 -0.0067311829
Feature25 -0.0488808852
Feature26 -0.0040028665
Feature27 0.0286530927
Feature28 -0.0136744813
Feature29 0.0086380442
Feature30 -0.0336064180
计算样本C-index
最佳lambda参数下构建模型的C-index值,反应模型的预测精度,越高越好1
2pred <- predict(fit, newx = prof)
apply(pred, 2, Cindex, y=phen)1
2 s0
0.7344005
参考
参考文章如引起任何侵权问题,可以与我联系,谢谢。
参考文章如引起任何侵权问题,可以与我联系,谢谢。