Dear R community, I've been writing simple functions for the past year and half and have come across a similar problem several times. The execution of a function within my own function produces NaN's or fails to execute as intended. My conundrum is that I can execute the function outside of my function without error, so it's difficult for me, as a novice functioneer, to figure out what's going wrong.
Here's my example for deltaMethod(): t <- c(0.00000, 26.24551, 61.78180, 86.88254, 221.75480) m <- c(48.591707, 15.655895, 12.284635, 5.758547, 0.000000) dat <- data.frame(t = t, m = m) m0 <- m[1] t0 <- t[5] nlls <- nls(m ~ (m0^(1/lambda) - (t * m0/t0)^(1/lambda))^lambda, start = list(lambda = 1), data = dat) xVal <- seq(0, t0, length = 10) mod.SE <- list() for(i in 1:length(xVal)){ z <- xVal[i] mod.SE[[i]] <- deltaMethod(nlls, "(m0^(1/lambda) - (z * m0/t0)^(1/lambda))^lambda")$SE } mod.SE This code executes deltaMethod() flawlessly (NOTE: [[1]] and [[10]] are supposed to be NaN's). However, my goal is to embed the deltaMethod inside another function I'm writing. For brevity's sake, here's a very simple example that produces the same problem. deltaSE <- function(mod, x){ mod.SE <- list() for(i in 1:length(xVal)){ z <- xVal[i] mod.SE[[i]] <- deltaMethod(mod, "(m0^(1/lambda) - (z * m0/t0)^(1/lambda))^lambda")$SE } mod.SE } deltaSE(nlls, xVal) deltaMethod, when embedded in my deltaSE function produces only NaN's. When I debug(deltaSE) and debug(deltaMethod), and then debug(deltaMethod.default) once delta method is debugging within deltaSE, I can see that eval() is producing a value of 0 for the estimate value of m. An m of 0 indeed would produce an NaN. I'm just not sure why this function is performing differently inside and outside my function. Any help for a lowly functioneer would be great! Patrick -- View this message in context: http://r.789695.n4.nabble.com/Trouble-embedding-functions-e-g-deltaMethod-in-other-functions-tp4662178.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.