To pass an Splus matrix to C, and then use it within C as a matrix, you
need to establish indexing. The example below is taken from the survival
library.
coxph.fit calls the C routine coxfit2 as
coxfit <- .C("coxfit2", iter=as.integer(maxiter),
as.integer(n),
as.integer(nvar), stime,
sstat,
x= x[sorted,] ,
etc...
where dim(x) = c(n, nvar).
The relevant code from coxfit2.c is below (every blank line below should
have "..." perhaps, to show that I've skipped over material not relevant to
this example). The S_dmatrix routine is part of Splus.
/*
** here is a cox regression program, written in C
** the input parameters are
**
** maxiter :number of iterations
** nused :number of people
** nvar :number of covariates
** time(n) :time of event or censoring for person i
** status(n) :status for the ith person 1=dead , 0=censored
** covar(nv,n) :covariates for person i.
** Note that S sends this in column major order.
......
double **S_dmatrix(double*, int, int);
void coxfit2(long *maxiter, long *nusedx, long *nvarx,
double *time, long *status, double *covar2,
int nused, nvar;
double **covar;
nused = *nusedx;
nvar = *nvarx;
covar= S_dmatrix(covar2, nused, nvar);
/*
** Subtract the mean from each covar, as this makes the regression
** much more stable
*/
for (i=0; i<nvar; i++) {
temp=0;
for (person=0; person<nused; person++) temp += covar[i][person];
temp /= nused;
means[i] = temp;
for (person=0; person<nused; person++) covar[i][person] -=temp;
}
-----
Last: I only work on one machine platform. Looking at S Programming by
Venables and Ripley, I see that some other internal S routines have name
differences in Windows and R. Is it S_dmatrix, Sdmatrix, just dmatrix,
...?
Someone else will have to answer that.
Terry Therneau
|