|
Hi every one. I have a exponential function (3 fitting parameters) that I would like to use to produce data (6 series) without having to use a loop. Here
wl = seq(300,500,1) k1 = c(1.2e-6, 4.9e-6, 9.6e-6, 2.7e-10, 6.7e-8, 7.44e-6) k2 = c(726, 352, 128, 5232, 1538, 128) k3 = c(-176, -224, -257, 88.7, -111, -256) stations = c('R5d', 'R5a', 'R9', '108', '406', '409') phiDIC = k1[1]*exp(k2[1]/(wl+k3[1])) #Here I would like to vectorize this function to create my 6 series instead of having to loop. Regards, Phil |
|
Perhaps ?outer -- well, not outer directly, but a multivariate outer
-- I keep this one around for personal use: `mouter` <- function(..., FUN = "*"){ dotArgs <- list(...) FUN <- match.fun(FUN) if(length(dotArgs) == 1L) return(unlist(dotArgs)) if (length(dotArgs) == 2L) return(do.call(base::outer, c(dotArgs, FUN = FUN))) base::outer(dotArgs[[1]], do.call(Recall, c(dotArgs[-1], FUN = FUN)), FUN) } So then something like: mouter(wl, k1, k2, k3, FUN = function(w, k1, k2, k3) k1 *exp(k2 / (w + k3))) if I understand your request correctly. Hope this helps, Michael On Thu, Apr 12, 2012 at 2:16 PM, Filoche <[hidden email]> wrote: > Hi every one. I have a exponential function (3 fitting parameters) that I > would like to use to produce data (6 series) without having to use a loop. > Here > > wl = seq(300,500,1) > > k1 = c(1.2e-6, 4.9e-6, 9.6e-6, 2.7e-10, 6.7e-8, 7.44e-6) > k2 = c(726, 352, 128, 5232, 1538, 128) > k3 = c(-176, -224, -257, 88.7, -111, -256) > > stations = c('R5d', 'R5a', 'R9', '108', '406', '409') > > > phiDIC = k1[1]*exp(k2[1]/(wl+k3[1])) #Here I would like to vectorize this > function to create my 6 series instead of having to loop. > > > Regards, > Phil > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-with-vectorization-tp4552638p4552638.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
Hi and thank you for your time.
I got this error when trying your function. mouter(wl, k1, k2, k3, FUN = function(w, k1, k2, k3) k1 *exp(k2 / (w + k3))) "Error in k3/(w + k3) : 'k3' is missing" Regards, Phil |
|
Strange, that isn't the error I get:
> mouter(wl, k1, k2, k3, FUN = function(w, k1, k2, k3) k1 *exp(k2 / (w + k3))) Error in FUN(X, Y, ...) : argument "k2" is missing, with no default Still, it's a problem with my mouter() function which was only tested on binary operators (and then only really to teach myself to use Recall()). This should work better: `mouter` <- function(..., FUN){ dotArgs <-as.list(do.call("expand.grid", list(..., KEEP.OUT.ATTRS= F))) names(dotArgs) <- names(formals(FUN)) ans <- do.call(FUN, dotArgs) dim(ans) <- sapply(list(...), length) ans } This only works when the function can take all the elements at a time though; I'm sure some fooling around could combine them nicely...it seems to test right on your data, but I haven't checked it more generally. Michael On Thu, Apr 12, 2012 at 3:29 PM, Filoche <[hidden email]> wrote: > Hi and thank you for your time. > > I got this error when trying your function. > > mouter(wl, k1, k2, k3, FUN = function(w, k1, k2, k3) k1 *exp(k2 / (w + k3))) > "Error in k3/(w + k3) : 'k3' is missing" > > Regards, > Phil > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-with-vectorization-tp4552638p4552833.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
Michael originally suggested ?outer. I think that was enough in this
case (no need for mv or sapply): wlpk3 <- outer(k3,wl,'+') ln.phiDIC <- log(k1)+k2/wlpk3 phiDIC<- t(exp(ln.phiDIC)) colnames(phiDIC)<- stations str(phiDIC) Cheers On Thu, Apr 12, 2012 at 8:58 PM, R. Michael Weylandt <[hidden email]> wrote: > Strange, that isn't the error I get: > >> mouter(wl, k1, k2, k3, FUN = function(w, k1, k2, k3) k1 *exp(k2 / (w + k3))) > Error in FUN(X, Y, ...) : argument "k2" is missing, with no default > > Still, it's a problem with my mouter() function which was only tested > on binary operators (and then only really to teach myself to use > Recall()). This should work better: > > `mouter` <- function(..., FUN){ > dotArgs <-as.list(do.call("expand.grid", list(..., KEEP.OUT.ATTRS= F))) > names(dotArgs) <- names(formals(FUN)) > ans <- do.call(FUN, dotArgs) > dim(ans) <- sapply(list(...), length) > ans > } > > This only works when the function can take all the elements at a time > though; I'm sure some fooling around could combine them nicely...it > seems to test right on your data, but I haven't checked it more > generally. > > Michael > > On Thu, Apr 12, 2012 at 3:29 PM, Filoche <[hidden email]> wrote: >> Hi and thank you for your time. >> >> I got this error when trying your function. >> >> mouter(wl, k1, k2, k3, FUN = function(w, k1, k2, k3) k1 *exp(k2 / (w + k3))) >> "Error in k3/(w + k3) : 'k3' is missing" >> >> Regards, >> Phil >> >> -- >> View this message in context: http://r.789695.n4.nabble.com/Help-with-vectorization-tp4552638p4552833.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> [hidden email] 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. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
