示例#1
0
def test_coxph():
    """
    Check that Cox results agree with R
    """
    tol = 1.e-5
    R_code = """
    library(selectiveInference)
    set.seed(43)
    n = 50
    p = 10
    sigma = 1.1

    x = matrix(rnorm(n*p),n,p)
    x=scale(x,TRUE,TRUE)

    beta = c(3,2,rep(0,p-2))
    tim = as.vector(x%*%beta + sigma*rnorm(n))
    tim= tim-min(tim)+1
    status=sample(c(0,1),size=n,replace=T)
    # first run glmnet


    gfit = glmnet(x,Surv(tim,status),standardize=FALSE,family="cox", thresh=1.e-14)
    # extract coef for a given lambda; note the 1/n factor!

    lambda = 1.5
    beta_hat = as.numeric(coef(gfit, s=lambda/n, exact=TRUE))
    # compute fixed lambda p-values and selection intervals
    out = fixedLassoInf(x,tim,beta_hat,lambda,status=status,family="cox")
    pval = out$pv
    vars_cox = out$var

    """

    rpy.r(R_code)
    R_pvals = np.asarray(rpy.r('pval'))
    selected_vars = np.asarray(rpy.r('vars_cox'))
    tim = np.asarray(rpy.r('tim'))
    tim = tim.reshape(-1)

    status = np.asarray(rpy.r('status'))
    status = status.reshape(-1)

    beta_hat = np.asarray(rpy.r('beta_hat'))
    x = np.asarray(rpy.r('x'))

    L = lasso.coxph(x, tim, status, 1.5)
    beta2 = L.fit()

    G1 = L.loglike.gradient(beta_hat)
    G2 = L.loglike.gradient(beta2)

    print(G1, 'glmnet')
    print(G2, 'regreg')

    yield np.testing.assert_equal, np.array(L.active) + 1, selected_vars
    yield np.testing.assert_allclose, beta2, beta_hat, tol, tol, False, 'cox coeff'
    yield np.testing.assert_allclose, L.summary(
        'onesided')['pval'], R_pvals, tol, tol, False, 'cox pvalues'
def test_coxph():
    tol = 1.e-5
    R_code = """
    library(selectiveInference)
    set.seed(43)
    n = 50
    p = 10
    sigma = 1.1

    x = matrix(rnorm(n*p),n,p)
    x=scale(x,TRUE,TRUE)

    beta = c(3,2,rep(0,p-2))
    tim = as.vector(x%*%beta + sigma*rnorm(n))
    tim= tim-min(tim)+1
    status=sample(c(0,1),size=n,replace=T)
    # first run glmnet


    gfit = glmnet(x,Surv(tim,status),standardize=FALSE,family="cox", thresh=1.e-14)
    # extract coef for a given lambda; note the 1/n factor!

    lambda = 1.5
    beta_hat = as.numeric(coef(gfit, s=lambda/n, exact=TRUE))
    # compute fixed lambda p-values and selection intervals
    out = fixedLassoInf(x,tim,beta_hat,lambda,status=status,family="cox")
    pval = out$pv
    vars_cox = out$var

    """

    rpy.r(R_code)
    R_pvals = np.asarray(rpy.r('pval'))
    selected_vars = np.asarray(rpy.r('vars_cox'))
    tim = np.asarray(rpy.r('tim'))
    tim = tim.reshape(-1)

    status = np.asarray(rpy.r('status'))
    status = status.reshape(-1)

    beta_hat = np.asarray(rpy.r('beta_hat'))
    x = np.asarray(rpy.r('x'))

    L = lasso.coxph(x, tim, status, 1.5)
    beta2 = L.fit()

    G1 = L.loglike.gradient(beta_hat)
    G2 = L.loglike.gradient(beta2)

    print(G1, 'glmnet')
    print(G2, 'regreg')

    yield np.testing.assert_equal, L.active + 1, selected_vars
    yield np.testing.assert_allclose, beta2, beta_hat, tol, tol, False, 'cox coeff'
    yield np.testing.assert_allclose, L.summary('onesided')['pval'], R_pvals, tol, tol, False, 'cox pvalues'
示例#3
0
def test_coxph():

    Q = rr.identity_quadratic(0.01, 0, np.ones(5), 0)
    X = np.random.standard_normal((100, 5))
    T = np.random.standard_exponential(100)
    S = np.random.binomial(1, 0.5, size=(100, ))

    L = lasso.coxph(X, T, S, 0.1, quadratic=Q)
    L.fit()

    L = lasso.coxph(X, T, S, 0.1, quadratic=Q)
    L.fit()

    C = L.constraints

    np.testing.assert_array_less( \
        np.dot(L.constraints.linear_part, L.onestep_estimator),
        L.constraints.offset)

    P = L.summary()['pval']

    return L, C, P
示例#4
0
def test_coxph():

    Q = rr.identity_quadratic(0.01, 0, np.ones(5), 0)
    X = np.random.standard_normal((100,5))
    T = np.random.standard_exponential(100)
    S = np.random.binomial(1, 0.5, size=(100,))

    L = lasso.coxph(X, T, S, 0.1, quadratic=Q)
    L.fit()

    L = lasso.coxph(X, T, S, 0.1, quadratic=Q)
    L.fit()

    C = L.constraints

    np.testing.assert_array_less( \
        np.dot(L.constraints.linear_part, L.onestep_estimator),
        L.constraints.offset)

    P = L.summary()['pval']

    return L, C, P
def test_coxph():

    Q = identity_quadratic(0.01, 0, np.ones(5), 0)
    X = np.random.standard_normal((100,5))
    T = np.random.standard_exponential(100)
    S = np.random.binomial(1, 0.5, size=(100,))

    L = lasso.coxph(X, T, S, 0.1, quadratic=Q)
    L.fit()
    C = L.constraints

    np.testing.assert_array_less( \
        np.dot(L.constraints.linear_part, L._onestep),
        L.constraints.offset)

    I = L.intervals
    P = L.active_pvalues

    return L, C, I, P