-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvcoxnet.m
87 lines (75 loc) · 2.28 KB
/
cvcoxnet.m
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function result = cvcoxnet(object, lambda, x, y, weights, offset, foldid, ...
type, grouped, keep)
% Internal glmnet function. See also cvglmnet.
if nargin < 10 || isempty(keep)
keep = false;
end
typenames = struct('deviance','Partial Likelihood Deviance');
if strcmp(type, 'default')
type = 'deviance';
end
if ~any(strcmp(type, {'deviance'}))
warning('Only ''deviance'' available for Cox models; changed to type=''deviance''');
type = 'deviance';
end
if isempty(offset)
offset = zeros(size(x,1),1);
end
nfolds = max(foldid);
if (length(weights)/nfolds < 10) && ~grouped
warning('Option grouped=true enforced for cv.coxnet, since < 3 observations per fold');
grouped = true;
end
cvraw = NaN(nfolds,length(lambda));
for i = 1:nfolds
which = foldid == i;
fitobj = object{i};
coefmat = glmnetPredict(fitobj,[],[],'coefficients');
if (grouped)
plfull = cox_deviance([],y,x,offset,weights,coefmat);
plminusk = cox_deviance([],y(~which,:),x(~which,:),offset(~which),...
weights(~which),coefmat);
cvraw(i,1:length(plfull)) = plfull - plminusk;
else
plk = cox_deviance([],y(which,:),x(which,:),offset(which),...
weights(which),coefmat);
cvraw(i,1:length(plk)) = plk;
end
end
status = y(:,2);
N = nfolds - sum(isnan(cvraw),1);
weights = accumarray(reshape(foldid,[],1),weights.*status);
cvraw = bsxfun(@rdivide,cvraw,weights); %even some weight = 0 does matter because of adjustment in wtmean!
cvm = wtmean(cvraw,weights);
sqccv = (bsxfun(@minus,cvraw,cvm)).^2;
cvsd = sqrt(wtmean(sqccv,weights)./(N-1));
result.cvm = cvm; result.cvsd = cvsd; result.name = typenames.(type);
if (keep)
warning('keep=TRUE not implemented for coxnet');
end
function result = cox_deviance(pred, y, x, offset, weights, beta)
ty = y(:,1);
tevent = y(:,2);
nobs = length(ty); nvars = size(x,2);
if isempty(weights)
weights = ones(nobs,1);
end
if isempty(offset)
offset = zeros(nobs,1);
end
if isempty(beta)
beta = []; nvec = 1; nvars = 0;
else
nvec = size(beta,2);
end
task = 42;
[flog, jerr] = glmnetMex(task,x,ty,tevent,offset,weights,nvec,beta);
if (jerr ~= 0)
errmsg = err(jerr,0,0,'cox');
if (errmsg.fatal)
error(errmsg.msg);
else
warning(errmsg.msg);
end
end
result = -2 * flog;