Search code examples
rconfidence-intervalnlsnon-linear-regression

Extract confidence intervals from an nlmrt object


NOTE: there are 2 huge objects in this question, in order to make the question reproducible. You don't need to read the declaration of the objects line by line to answer the question: their declaration (obtained with dump("x", file=stdout()) ) is only provided so that you can load the objects in your R environment and inspect them.

I have this object of class "nlmrt" (sorry, it's huge):

library(nlmrt)
results.nlmrt <-
structure(list(resid = c(-1.873797, -1.082639, -3.128738, -2.065225, 
10.036262, 9.038775, -0.557938, -0.0227550000000001, -1.541254, 
-0.617675999999999, -2.193645, -0.939828000000002, 0.81962, 0.856692, 
1.588436, 1.7647, -10.546283, 3.360661, -0.835847, -0.578092, 
-1.147379, -0.930261999999999, -1.252843, -2.1055, 1.347841, 
-0.329777, -4.147924, -3.676401, 5.342006, 5.818819, -0.225925, 
-0.058781999999999, 0.334197, 0.4505, 1.017727, 0.669381000000001, 
1.884993, 1.454699, 1.67084, 1.755269, -0.471676, -0.524057, 
-3.373085, -3.278019, -6.219445, -7.161793, -10.737809, -8.693415, 
-2.840445, -3.136342, -3.662787, -5.37458, 5.945885, 5.213688, 
-3.884115, -3.71474, -6.818322, -6.348495, -11.975686, -8.800068, 
5.806859, 5.054232, -2.531185, -2.013974, -3.746896, -3.020429, 
-1.304244, -1.113971, -2.777007, -2.837965, -5.07189, -5.658287, 
-1.151053, -0.947455, -2.547545, -2.744747, 0.790208, 0.546432, 
1.517014, 1.102288, 1.249165, 1.011463, 0.126482000000003, 0.136798000000002, 
0.28296, 0.225576, -0.284154, -0.15046, 1.068714, 0.816097, 1.561979, 
1.208949, -3.193332, -2.120788, -2.501207, -1.75678, -2.265861, 
-1.396114, 6.420019, 7.175985, 3.453049, 4.296639, 1.910447, 
2.479853, 1.147497, 1.288324, 5.996762, 5.92161, 3.146104, 3.158998, 
1.668787, 1.702539, 2.113205, 1.845771, 1.327114, 1.440344, 0.855292, 
7.73017, 7.746381, 4.152009, 4.183986, 2.682163, 2.957113, 2.189862, 
2.137862, 6.977142, 6.592122, 1.520254, 1.794167, 10.447364, 
3.10818, 5.574112, 5.101422, 1.407211, 1.852486, -0.278430999999999, 
-0.0941270000000003, -1.314603, -1.201795, 0.934806999999999, 
1.114106, 0.63108, 0.722106, -5.211555, -5.645131, 0.864519, 
0.7813243, 0.390436, 0.024733, -1.043325, -2.48613, -1.85254, 
-2.045719, -2.385649, -2.223719, -3.935888, -2.720326, -2.658094, 
-1.243398, -1.903095, -0.581753, -2.544048, -4.201287, -4.221324, 
-4.733332, -3.42192, -7.584243, -5.434314, -2.059307, -1.756377, 
-4.803778, -0.601198, -2.301358, -3.166855, -0.628322000000001, 
-2.278394, -2.974249, -1.86614, -1.244455, -0.89637, -5.43132, 
-5.769175, -8.980463, -10.269705, -1.49086, -1.077036, -2.536101, 
-2.301336, -6.2600988, -5.661762, -0.400368, -0.43933, -1.538335, 
-2.086426, -5.888978, -6.759651, -0.428561, -0.977354, -3.140401, 
-3.14137, -1.224751, -1.418533, -5.543907, -6.148431, -3.763996, 
-3.526406, -4.757332, -4.234969, -2.743054, -2.236952, -0.819221, 
-0.932174), jacobian = structure(c(-3.17994601613073, -3.17994601613073, 
0, -15.8997300798063, 0, -15.8997300798063, -1.58997300806537, 
-1.58997300806537, -3.17994601584829, -3.17994601584829, 0, 0, 
1.58997300792415, 1.58997300799476, 3.17994601584829, 3.17994601613073, 
15.8997300798063, 0, -1.58997300792415, -1.58997300806537, 0, 
0, 0, 0, -15.8997300792415, -15.8997300792415, 3.17994601613073, 
4.76991902377244, 3.17994601598951, 1.58997300792415, -4.76991902405488, 
-4.76991902405488, -3.17994601584829, -3.17994601598951, 0, 0, 
-15.8997300798063, 0, 0, 0, -1.58997300792415, -1.58997300792415, 
-1.58997300806537, -1.58997300792415, 0, 0, 0, 0, -1.58997300806537, 
-3.17994601598951, -15.8997300798063, -15.8997300798063, 1.58997300792415, 
1.58997300792415, -1.58997300806537, -3.17994601598951, 0, 0, 
-15.8997300798063, 0, 1.58997300792415, 3.17994601584829, -1.58997300806537, 
-1.58997300792415, 0, -15.8997300798063, 0, -1.58997300792415, 
-1.58997300792415, -1.58997300806537, -15.8997300798063, 0, -1.58997300799476, 
-1.58997300799476, -3.17994601598951, -3.17994601598951, 0, 0, 
1.58997300792415, 1.58997300806537, 0, 0, 0, 0, 1.58997300799476, 
1.58997300799476, 1.58997300799476, 0, 1.58997300799476, 0, 1.58997300792415, 
3.17994601598951, -15.8997300798063, 0, -4.76991902391366, -3.17994601598951, 
0, -15.8997300798063, 0, 0, 0, 0, -1.58997300792415, -3.17994601584829, 
-1.58997300792415, -1.58997300799476, 0, 0, -3.17994601584829, 
-1.58997300792415, -1.58997300792415, -1.58997300792415, 0, 0, 
3.17994601598951, 1.58997300792415, 1.58997300799476, -15.8997300798063, 
0, -3.17994601584829, -3.17994601613073, 0, 0, -1.58997300792415, 
-1.58997300792415, 0, -15.8997300798063, -1.58997300806537, 0, 
0, 0, 0, 0, 1.58997300799476, 1.58997300799476, 3.17994601613073, 
3.17994601584829, 15.8997300798063, 0, 3.17994601598951, 1.58997300792415, 
1.58997300799476, 1.58997300799476, 0, -1.58997300792415, 0.158997300803006, 
0.174897030874481, 0.476991902391366, 0.476991902409018, 1.58997300799476, 
1.58997300806537, -0.158997300785354, 0, 0, 0, 0, 0, 0, 0, 0, 
-1.58997300806537, 0, 0.635989203141415, 0.794986504103292, 1.11298110560339, 
0, 0, -1.58997300806537, 0.635989203212024, 0, 0.635989203282633, 
0.794986503979726, 0, 0, -4.76991902391366, -4.76991902391366, 
0, -1.58997300792415, -1.58997300806537, 0, -1.58997300806537, 
-1.58997300792415, 0, 0, 0.476991902391366, 0.476991902391366, 
0.794986503962074, 0.794986503962074, 1.81256922910131, 2.06696491038612, 
-1.58997300799476, -1.58997300799476, -1.58997300806537, -1.58997300806537, 
-1.58997300792415, 0, -6.35989203197903, -6.35989203197903, 0, 
0, 0, 0, 0, 0, 0, 0, 1.58997300792415, 1.58997300792415, 1.58997300792415, 
1.58997300806537, 0.158997300803006, 0.476991902391366, -13.7270193342762, 
-12.2017949638612, -15.2522437046911, -30.5044874093822, -30.5044874093822, 
-45.7567311146151, -6.10089748193062, -7.62612185234555, -13.7270193340052, 
-13.7270193342762, -30.5044874099241, -30.5044874093822, 6.10089748193062, 
6.10089748193062, 12.2017949635903, 13.7270193342762, 45.7567311140733, 
30.5044874093822, -6.10089748193062, -6.10089748193062, -15.2522437046911, 
-15.252243704962, -30.5044874093822, -30.5044874099241, -45.7567311135314, 
-45.7567311135314, 13.7270193342762, 15.2522437046911, 9.15134622289594, 
9.15134622289594, -18.3026924457919, -18.3026924457919, -10.6765705931754, 
-12.2017949637258, -45.7567311140733, 1082.90930303524, -30.5044874093822, 
-15.2522437046911, -15.2522437046911, -15.2522437046911, -7.62612185234555, 
-6.10089748179516, -7.62612185234555, -7.62612185234555, -15.2522437046911, 
-15.2522437046911, -30.5044874093822, -30.5044874099241, -7.62612185234555, 
-7.62612185234555, -15.2522437046911, -15.2522437046911, 7.62612185234555, 
6.10089748193062, -7.62612185248101, -7.62612185234555, -15.2522437046911, 
-15.2522437046911, -30.5044874093822, -15.252243705233, 6.10089748193062, 
7.62612185234555, -7.62612185234555, -7.62612185234555, -15.2522437046911, 
-30.5044874093822, -4.57567311144797, -4.57567311138023, -7.62612185234555, 
-7.62612185234555, -30.5044874093822, -15.2522437046911, -4.57567311144797, 
-4.57567311138023, -10.6765705933109, -9.15134622289594, 3.05044874096531, 
3.05044874096531, 7.62612185234555, 7.62612185234555, 15.2522437046911, 
15.2522437046911, 30.5044874093822, 30.5044874093822, 4.57567311144797, 
4.57567311138023, 4.57567311144797, 4.57567311144797, 4.57567311144797, 
3.05044874096531, 7.62612185234555, 10.6765705933109, -30.5044874093822, 
-30.5044874096531, -13.7270193342762, -13.7270193342762, -30.5044874093822, 
-30.5044874093822, -30.5044874093822, -30.5044874093822, -15.2522437046911, 
-15.2522437046911, -10.6765705931754, -10.6765705931754, -6.10089748179516, 
-4.57567311138023, -15.2522437046911, -15.2522437046911, -12.2017949635903, 
-10.6765705931754, -6.10089748193062, -6.10089748193062, 15.252243704962, 
15.2522437046911, 10.6765705933109, 9.15134622289594, 6.10089748193062, 
-30.5044874093822, -15.2522437046911, -10.6765705931754, -10.6765705934463, 
-4.57567311138023, -4.5756731115157, -4.57567311138023, -6.10089748179516, 
-30.5044874093822, -30.5044874096531, -6.10089748193062, -4.57567311138023, 
-15.2522437046911, -15.2522437046911, -15.2522437046911, -30.5044874096531, 
6.10089748186289, 4.57567311144797, 13.7270193342762, 13.7270193342762, 
30.5044874096531, 15.2522437046911, 10.6765705933109, 10.6765705933109, 
4.57567311144797, 6.10089748193062, -1.52522437055039, -4.5756731115157, 
0.457567311138023, 0.610089748189676, 1.98279168160375, 1.83026924456903, 
6.10089748193062, 6.10089748193062, -0.305044874069438, 0, -1.52522437041492, 
-1.52522437048266, -1.52522437048266, 0, -1.52522437048266, 0, 
0, -3.05044874096531, -1.52522437048266, 2.44035899269097, 2.59288142989502, 
4.11810580030994, 4.57567311138023, -4.57567311138023, -6.10089748193062, 
2.13531411868927, 1.52522437048266, 2.59288142989502, 2.89792630389673, 
0, 1.52522437048266, -21.3531411866217, -19.8279168160713, -1.52522437041492, 
-4.57567311138023, -7.62612185234555, -3.05044874089758, -7.62612185234555, 
-6.10089748179516, 0, -15.2522437046911, 1.52522437048266, 1.67774680755124, 
2.59288142975955, 2.59288142975955, 6.68048274270997, 7.47359941527696, 
-4.57567311138023, -4.57567311138023, -4.57567311138023, -4.57567311138023, 
-4.57567311138023, -4.57567311138023, -25.9288142978665, -25.9288142981374, 
0, -1.52522437055039, 0, -1.52522437048266, -3.05044874096531, 
-3.05044874110078, 1.52522437048266, 3.05044874096531, 4.5756731115157, 
4.57567311138023, 7.62612185234555, 9.15134622289594, 0.762612185241328, 
1.22017949637935), .Dim = c(212L, 2L)), feval = 31, jeval = 14, 
    coefficients = structure(c(0.628931491356317, -0.655631241727379
    ), .Names = c("ffactor_Ns", "ffactor_Ms")), ssquares = 3107.43103734174, 
    lower = c(-1, -1), upper = c(1, 1), maskidx = NULL), .Names = c("resid", 
"jacobian", "feval", "jeval", "coefficients", "ssquares", "lower", 
"upper", "maskidx"), class = "nlmrt")

I need to extract confidence intervals for the two parameters ffactor_Ns and ffactor_Ms. If I print the object I can see the standard error on screen, which can be used to compute confidence intervals:

> print(results.nlmrt)
nlmrt class object: x 
residual sumsquares =  3107.4  on  212 observations
    after  14    Jacobian and  31 function evaluations
  name            coeff          SE       tstat      pval      gradient    JSingval   
ffactor_Ns      0.628931       0.05673      11.09          0       101.8        1106  
ffactor_Ms     -0.655631      0.003512     -186.7          0       913.1        67.8  

However, I need the confidence intervals to be computed automatically: I cannot count on someone being in front of the screen each time I run the code. With your help, I was able to convert the object to an object of class "nls", which has a confint method:

library(nls2)
nls_object <-
structure(list(m = structure(list(resid = function () 
resid, fitted = function () 
rhs, formula = function () 
form, deviance = function () 
dev, lhs = function () 
lhs, gradient = function () 
.swts * attr(rhs, "gradient"), conv = function () 
{
    if (npar == 0) 
        return(0)
    rr <- qr.qty(QR, resid)
    sqrt(sum(rr[1L:npar]^2)/sum(rr[-(1L:npar)]^2))
}, incr = function () 
qr.coef(QR, resid), setVarying = function (vary = rep_len(TRUE, 
    length(useParams))) 
{
    assign("useParams", if (is.character(vary)) {
        temp <- logical(length(useParams))
        temp[unlist(ind[vary])] <- TRUE
        temp
    }
    else if (is.logical(vary) && length(vary) != length(useParams)) 
        stop("setVarying : 'vary' length must match length of parameters")
    else {
        vary
    }, envir = thisEnv)
    gradCall[[length(gradCall) - 1L]] <<- useParams
    if (all(useParams)) {
        assign("setPars", setPars.noVarying, envir = thisEnv)
        assign("getPars", getPars.noVarying, envir = thisEnv)
        assign("getRHS", getRHS.noVarying, envir = thisEnv)
        assign("npar", length(useParams), envir = thisEnv)
    }
    else {
        assign("setPars", setPars.varying, envir = thisEnv)
        assign("getPars", getPars.varying, envir = thisEnv)
        assign("getRHS", getRHS.varying, envir = thisEnv)
        assign("npar", length(seq_along(useParams)[useParams]), 
            envir = thisEnv)
    }
}, setPars = function (newPars) 
{
    setPars(newPars)
    assign("resid", .swts * (lhs - assign("rhs", getRHS(), envir = thisEnv)), 
        envir = thisEnv)
    assign("dev", sum(resid^2), envir = thisEnv)
    assign("QR", qr(.swts * attr(rhs, "gradient")), envir = thisEnv)
    return(QR$rank < min(dim(QR$qr)))
}, getPars = function () 
getPars(), getAllPars = function () 
getPars(), getEnv = function () 
env, trace = function () 
{
    cat(format(dev), ": ", format(getPars()))
    cat("\n")
}, Rmat = function () 
qr.R(QR), predict = function (newdata = list(), qr = FALSE) 
eval(form[[3L]], as.list(newdata), env)), .Names = c("resid", 
"fitted", "formula", "deviance", "lhs", "gradient", "conv", "incr", 
"setVarying", "setPars", "getPars", "getAllPars", "getEnv", "trace", 
"Rmat", "predict"), class = "nlsModel"), call = quote(nls2(formula = model_string, 
    start = coef(results.nlmrt), algorithm = "brute")), convInfo = structure(list(
    isConv = TRUE, finIter = 2L, finTol = NA), .Names = c("isConv", 
"finIter", "finTol"))), .Names = c("m", "call", "convInfo"), class = "nls")

Unfortunately, the confint method is not working!

> confint(nls_object)
Waiting for profiling to be done...
Error in chol2inv(object$m$Rmat()) : 
  element (2, 2) is zero, so the inverse cannot be computed

I have no idea what this $m$Rmat method is supposed to do, but it seems to compute some matrix with only one non-zero element:

> nls_object$m$Rmat()
        [,1] [,2]
[1,] -1028.7    0
[2,]     0.0    0

I'm at the end of my rope. The SE (and thus the confidence interval) must be buried somewhere inside results.nlrmt, otherwise print couldn't show it. Why is so darn difficult to extract it?


Solution

  • Thanks to @Roland's comment, I found out that the solution is actually very simple. To extract standard errors, t-statistics or p-values from an object of class "nlmrt", just do the following:

    library(nlmrt)
    goofy <- summary(results.nlmrt)
    #standard errors
    goofy$SEs
    #t-statistics
    goofy$tstat
    #p-values
    goofy$pval
    

    Of course, summary(results.nlmrt) was one of the first things I tried, but since, by itself, it doesn't return anything to screen, and since ?summary.nlmrt says it returns an invisible copy of the nlmrt object, I didn't think it could return the stats I was looking for.