24859 15

[统计软件] R语言glmnet中的cox回归) [推广有奖]

  • 0关注
  • 2粉丝

本科生

78%

还不是VIP/贵宾

-

威望
0
论坛币
62 个
通用积分
0
学术水平
7 点
热心指数
4 点
信用等级
0 点
经验
1953 点
帖子
115
精华
0
在线时间
63 小时
注册时间
2013-12-26
最后登录
2016-4-5

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
> library("glmnet")
> library("survival")
> data<-read.csv("F:yang\\yang.csv")
> dim(data)

[1] 560  18
> head(data)
  time status hsa.miR.198 hsv1.miR.H1  GALNT10   KLHDC3  MGC3196    PPM2C
1 1336      0    6.000120    7.115827 4.211642 6.430065 7.368874 6.611384
2 1247      0    5.668861    6.080477 4.819239 7.095121 7.902691 6.975410
3   55      0    5.584859    5.799768 4.349560 6.035969 8.856912 5.458948
4 1495      1    4.539358    4.346842 3.903397 6.708086 8.263683 6.004740
5   61      0    4.810125    4.838093 5.520588 5.452923 7.802564 6.185189
6 1418      1    5.490129    5.790943 4.376609 7.073469 8.070348 5.691560(部分数据省略)
岭回归做法:
> x<-as.matrix(data[,3:18])
> y<-data[,1]
> cv.fit<-cv.glmnet(x,y,family="gaussian",maxit=1000,nfold=10,alpha=0)
> plot(cv.fit)

请问如何用family=“cox”做?我用以下代码,提示错误
> cv.fit<-cv.glmnet(x,Surv(time,status),family="cox",maxit=1000,nfold=10,alpha=0)
Error in drop(y) :
  error in evaluating the argument 'x' in selecting a method for function 'drop': Error in Surv(time, status) : Time variable is not numeric

> y=cbind(time=data[,1],status=data[,2])
> cv.fit<-cv.glmnet(x,y,family="cox",maxit=1000,nfold=10,alpha=0)

Error in coxnet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  :
  negative event times encountered;  not permitted for Cox family

请指点,不胜感激!


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:glmnet MNE Cox NET R语言 survival library status

已有 1 人评分经验 学术水平 收起 理由
紫霄香榭 + 12 + 1 鼓励积极发帖讨论

总评分: 经验 + 12  学术水平 + 1   查看全部评分

沙发
melodyonly 发表于 2014-6-8 13:55:37 |只看作者 |坛友微信交流群
想学习学习

使用道具

恩,找到错误了。O(∩_∩)O~

使用道具

melodyonly 发表于 2014-6-8 13:55
想学习学习
昵称让我想起身边的一个同学,哈哈melody

使用道具

> options(stringAsFactors=F)
> Surv(data$time,data$vital_status)
> class(Surv(data$time,data$vital_status))
[1] "Surv"
> surv<-Surv(data$time,data$vital_status)
> dat<-data[,-c(1,2)]
> fix(data)
> fix(dat)
> x<-as.matrix(dat)
> cv.fit<-cv.glmnet(x,surv,family="cox",maxit=1000,alpha=0)
> plot(cv.fit)
> fit<-glmnet(x,surv,family="cox",maxit=1000,alpha=0)
> coefficients<-coef(fit,s=cv.fit$lambda.min)
> coefficients

使用道具

地板
PhoebeSG 发表于 2015-6-11 00:17:32 |只看作者 |坛友微信交流群
想请问楼主这个“negative event times encountered;  not permitted for Cox family”问题最后是怎么解决的呀?我也是遇到了这个,并不太懂。。。

使用道具

7
pzqmoon 发表于 2016-7-20 16:04:59 |只看作者 |坛友微信交流群
相逢一笑0206 发表于 2014-6-8 22:38
> options(stringAsFactors=F)
> Surv(data$time,data$vital_status)
> class(Surv(data$time,data$vital ...
请问怎么解决的,谢谢

使用道具

8
yangjw5 发表于 2016-9-11 16:19:26 |只看作者 |坛友微信交流群
PhoebeSG 发表于 2015-6-11 00:17
想请问楼主这个“negative event times encountered;  not permitted for Cox family”问题最后是怎么解决的 ...
详细代码参见这个网站https://github.com/jeffwong/glmnet/blob/master/R/coxnet.R
要事先把TIME和STATUS转换为DOUBLE型,原始数据可能是INTEGER,所以要进行数据类型转换,不然就会报错(Error in if (any(ty <= 0)) stop("negative event times encountered;  not permitted for Cox family") :
  missing value where TRUE/FALSE needed)。

我写的代码如下:
install.packages("glmnet")
library(glmnet) #载入弹性网络R包
library(survival)
w=read.csv("C:\\Users\\yangjiang\\Desktop\\R\\Stock1.csv")
x=as.matrix(w[,1:30]);
y=as.double(w$TIME)
STATUS=as.double(w$STATUS)
surv=Surv(y,STATUS);
cv.fit<-cv.glmnet(x,surv,family="cox") #交叉验证过程
plot(cv.fit) #交叉验证图
这样运行就没出错。
希望可以帮到你!
已有 1 人评分学术水平 热心指数 收起 理由
PhoebeSG + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1   查看全部评分

使用道具

9
天天甜甜fly 发表于 2016-9-19 16:58:53 |只看作者 |坛友微信交流群
你好,我第一次用glmnet,想知道这个data和status分别指的是什么啊,你这两个问题我都遇到了,可是不知道如何解决,也对各个变量进行double处理了,可是还是报错,我的数据的所有变量和label都是0,1的,这是不是问题的所在啊

使用道具

10
raindk 发表于 2018-11-15 10:17:28 |只看作者 |坛友微信交流群
PhoebeSG 发表于 2015-6-11 00:17
想请问楼主这个“negative event times encountered;  not permitted for Cox family”问题最后是怎么解决的 ...
我也出现了这个错误,是因为生存时间有0值,去掉0的生存时间就好使了

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-6-21 21:51