Question

I used the R package plyr to do some nonlinear regressions, and now that I have a list of nonlinear regression fits, I'd like to put the p values and 95% confidence intervals of those fits into a data.frame. When I do that, though, I keep getting the error "Error: $ operator is invalid for atomic vectors". Here are some made-up data where the model is y = A/x and the plyr command that I used to do the fits:

 set.seed(253)
 Data <- data.frame("Group"=rep(c("Tx1", "Tx2", "Tx3", "Healthy"), each=14),
               "X"=rep(c(5,6,7,8,9,10,12.5,15,17.5,20,22.5,25,27.5,30),4),
               "A.obs"=c(rnorm(14, mean=500, sd=80),
                         rnorm(14, mean=250, sd=30),
                         rnorm(14, mean=350, sd=75),
                         rnorm(14, mean=50, sd=5)))
 Data$Y <- Data$A.obs/Data$X

 NLmodels <- dlply(Data, "Group", 
              function(x) nls(x$Y ~ A/x$X, data=x, start=list(A=50), 
                              algorithm="port", lower=list(A=50)))

Here is how I tried to extract the data from that list into a data.frame:

 lci <- function(x) suppressMessages(confint(x)[1]) # Lower 95% CI
 uci <- function(x) suppressMessages(confint(x)[2]) # Upper 95% CI
 pval <- function(x) summary(x)$coef[1,4]

 NLmodels.df <- ldply(NLmodels, function(x) coef(x), LCI=lci(x), UCI=uci(x), pvalue=pval(x))

I thought that the problem was that, normally, when you run confint() on a nls object, you get the message, "Waiting for profiling to be done…" and maybe that message was causing a glitch with the ldply() command. Hence the addition of suppressMessages(), but that didn't solve my problem. It still won't run and only gives me that error message.

Any ideas? Thank you in advance!

Was it helpful?

Solution

To avoid more complicated subsetting, try simplifying your summary function to:

ldply(NLmodels, function(x) c(coef(x),confint(x),p=summary(x)$coef[1,4]))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top