search for: fcn

Displaying 20 results from an estimated 56 matches for "fcn".

Did you mean: fc4
2007 Aug 22
3
integrate
Hi, I am trying to integrate a function which is approximately constant over the range of the integration. The function is as follows: > my.fcn = function(mu){ + m = 1000 + z = 0 + z.mse = 0 + for(i in 1:m){ + z[i] = rnorm(1, mu, 1) + z.mse = z.mse + (z[i] - mu)^2 + } + return(z.mse/m) + } > my.fcn(-10) [1] 1.021711 > my.fcn(10) [1] 0.9995235 > my.fcn(-5) [1] 1.012727 > my.fcn(5) [1] 1.033595 > my.fcn(0) [1] 1.106282 >...
2006 Apr 04
2
Return function from function with minimal environment
Hi, this relates to the question "How to set a former environment?" asked yesterday. What is the best way to to return a function with a minimal environment from a function? Here is a dummy example: foo <- function(huge) { scale <- mean(huge) function(x) { scale * x } } fcn <- foo(1:10e5) The problem with this approach is that the environment of 'fcn' does not only hold 'scale' but also the memory consuming object 'huge', i.e. env <- environment(fcn) ll(envir=env) # ll() from R.oo # member data.class dimension object.size # 1 huge...
2010 Jun 09
2
OOP and passing by value
...ful for the great effort the product and contributors are delivering. My question is as follows: I am trying to use S4 style classes but cannot write functions that modify an object because paramter passing is by value. For example I want to do this: setGeneric("setData", function(this,fcn,k){ standardGeneric("setData") }) setClass( "test", representation(f="numeric", t="numeric") ) setMethod("setData","test", function(this,fcn,k){ this@t <- as.numeric(seq(-k,k))/(2*k+1) this@f <- sapply(t,FUN=fcn) } )...
2016 Mar 13
1
formals(x)<- drops attributes including class
Just checking in to see whether it is intended or not that assigning new formals to a function/closure causes any attributes to be dropped: EXAMPLE: > fcn <- structure(function() {}, foo="foo", class=c("foo")) > str(fcn) function () - attr(*, "srcref")=Class 'srcref' atomic [1:8] 1 18 1 30 18 30 1 1 .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x0...
2011 Mar 15
1
Problem with nls.lm function of minpack.lm package.
...ot change as the algorithm tries to vary the parameters (the way the parameter vector changes is decided by either a finite difference method or the Jacobian function you supply). There are some strange things going on with your models; in the case of this call: out <- nls.lm(par = guess, fn = fcn, jac = fcn.jac, fcall = f.Th, jcall = j.Th, h = h, Th = Th, control = nls.lm.control(maxfev = integer(), maxiter = 10000, nprint=100)) The parameter estimates keep changing even after 1000 iterations. This is not good. In the second fit, I change the...
2008 Apr 22
4
how to convert non numeric data into numeric?
I am having the following error in my function function(theta,reqdIRR) { theta1<-theta[1] theta2<-theta[2] n<-length(reqdIRR) constant<- n*(theta1+theta2) sum1<-lapply(reqdIRR*exp(theta1),FUN = sum) sum2<-lapply(exp(theta2 - reqdIRR*exp(theta1)),FUN = sum) sum = sum1 + sum2 log.fcn = constant - as.numeric(sum) result = - log.fcn return(result) } *error : neg.log.gumbel(1,reqdIRR) Error in sum1 + sum2 : non-numeric argument to binary operator > how can i rectify the error?Its really urgent... * [[alternative HTML version deleted]]
2003 Oct 30
2
'nls' and its arguments
Dear R experts! I'd to fit data by 'nls' with me-supplied function 'fcn'. 1) I'd like 'fcn' to accept arbitrary arguments, i.e. I defined it as f(...) {<body>}. (Ok, that's not actually impotant). 2) Second, I would NOT like to supply every parameter in the formula. To illustrate this, let's look at the last example of 'nls'...
2012 Jan 25
1
solving nls
Hi, I have some data I want to fit with a non-linear function using nls, but it won't solve. > regresjon<-nls(lcfu~lN0+log10(1-(1-10^(k*t))^m), data=cfu_data, > start=(list(lN0 = 7.6, k = -0.08, m = 2))) Error in nls(lcfu ~ lN0 + log10(1 - (1 - 10^(k * t))^m), data = cfu_data, : step factor 0.000488281 reduced below 'minFactor' of 0.000976562 Tried to increase minFactor
2010 May 14
1
debugging substitute function in R 2.11
...of my most frequent calls in any R session (source added to my .Rprofile). However, since I upgraded to R 2.11, the function stopped working, retrieving the following error: ****** R> ll () Error in substitute({ : unused argument(s) (list = list(member = as.name(member), property = property, fcn = as.name(property))) ****** This seems to be due to a change in the substitute function in R 2.11, as described in the release notes: ****** Primitive functions UseMethod(), attr(), attr<-(), on.exit(), retracemem() and substitute() now use standard argument matching (rather than positional...
2007 Sep 29
2
Help with functions (iterations)
...store it in a numeric vector (of length 100). The for loop should be placed in a function that allows the user to vary the sample size, the simulation size, the integration limits and the input function. In addition construct a histogram of the estimates and include a red vertical line at pi. ex1.fcn<-function(x){ h<-4/(1+x^2) return(h) } n=1000 a=0 b=1 my.rand.x=runif(n,min=a,max=b) pi.MC = ((b-a)/n)*sum(ex1.fcn(my.rand.x))
2024 Feb 06
1
[EXTERNAL] Re: NOTE: multiple local function definitions for ?fun? with different formal arguments
Here's a dummy example that I think illustrates the problem: toto <- function() { if (runif(1) < 0.5) function(a) a else function(a,b) a+b } > fcn <- toto() > fcn(1,2) [1] 3 > fcn <- toto() > fcn(1,2) [1] 3 > fcn <- toto() > fcn(1,2) Error in fcn(1, 2) : unused argument (2) How can you use the returned function, if you get different arguments? In your example, you cannot use the returned function without knowing '...
2013 Feb 05
1
integrate: Don't do this?
When I run the following function HQ2 <- function(n) { nv <- 6 * sqrt(n) fcn <- function(z) { pchisq(z^2 / 36, n - 1) * dnorm(nv - z) } ## I want the integral from 0 to infinity: f.Inf <- integrate(fcn, 0, Inf) ## Doc: "Don't do this": f.100 <- integrate(fcn, 0, 100) cbind(f.Inf, f.100) } I get, for n = 9...
2005 Oct 09
3
[ subscripting sometimes loses names (PR#8192)
...do NOT replace them. TODO: Investigate fixing the # stock implementation instead, especially for R. # # --atp at piskorski.com, 2005/09/27 17:03 EDT # if (!.R.) { # For S-Plus: # First make sure that if you run this twice, you still get the # real original function: data.frame.original.fcn <- get("[.data.frame", where="splus") # For S-Plus (at least version 6.2.1) we do not need to override # the "[" function, it already does the right thing. # --atp at piskorski.com, 2005/07/01 10:11 EDT "[.data.frame" <- function(x ,... ,d...
2005 Sep 07
1
Tracebacks with tryCatch() and withCallingHandlers()?
...it seems to be case for withCallingHandlers() too). Does anyone know of a workaround for this? Is there a way to get the call stack within the condition handler? Example: foo <- function() stop("whoops"); pre <- function() foo(); bar <- function() pre(); yo <- function(fcn=tryCatch) { fcn(bar(), error = function(ex) { str(ex); traceback(); # I would like to access the call stack here! } ) traceback(); } Calling these gives: > rm(.Traceback) > yo() # Using tryCatch() List of 2 $ message: chr "whoops" $ call...
2001 Jul 03
1
extracting pkg fcn info
I would like to learn what functions are available in a given R package (say, the pkg sma) and what the arguments and return value is for each of those functions. Is there an R CMD available for that or a suggested method for easily extracting that information? thanks, Natalie. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read
2011 Dec 14
1
uniroot function question
...d then using uniroot to solve for the accompanying second solution, then graphing the two vectors. p0 = .36 f = function(x) 0.29 * exp(5.66*(x - p0)) f.integral = integrate(f, p0, 1) p1 = p0 + .01 i = 1 n = (1 - p0)/.01 p1.vector = rep(0,n) p2.vector = rep(0,n) for (i in 1:n) { p1.vector[i] = p1 fcn = function(p2) p1*f(p1) + (.20/5.66)*(exp(5.66*(p2 - p0)) - exp(5.66*(p1 - p0))) + (1 - p2)*f(p2) - as.numeric(f.integral$value) sol = uniroot(try, lower = p1, upper = 1) p2.vector[i] = p2 i = i+1 p1 = p1 + .01 } plot(p1.vector,p2.vector) p1, p2 both have to be between p0 and 1, p1 < p2. I...
2002 Jan 23
1
Question about substitute (and eval, parse and deparse)
...#39; and 'x' is replaced with 'object'. I'll use the variables 'name' and 'arg' for this purpose; name <- "foo"; arg <- "x"; Now I'll create the 'call' to be evaluate to define the new function: call <- substitute(fcn <- function(arg) { cat("Function", name, "was called with argument", arg, ".\n"); }, list=list(fcn=as.name(name), arg=as.name(arg), name=name)) Now looking at the call things look a little bit strange: > print(call) foo <- function(arg) { ca...
2006 Apr 05
1
predict.smooth.spline.fit and Recall() (Was: Re: Return function from function and Recall())
...this problem. > > getPredictor <- function(x, y) { > sp <- smooth.spline(x=x, y=y, keep.data=FALSE) > function(x, ...) predict(sp$fit, x, ...)$y > } > > # Simulate data > x <- 1:10 > y <- 1:10 + rnorm(length(x)) > > # Estimate predictor function > fcn <- getPredictor(x,y) > > # No extrapolation => no Recall() > ypred <- fcn(x) > print(ypred) > # Gives: # [1] 2.325181 2.756166 ... > > # With extrapolation => Recall() > xextrap <- c(0,x) > ypred <- fcn(xextrap) > # Gives: # Error in Recall(objec...
2024 Feb 06
1
[EXTERNAL] Re: NOTE: multiple local function definitions for ?fun? with different formal arguments
Because functions get called and therefore, the calling sequence matters. It?s just protecting you from yourself, but as someone pointed out, there?s a way to silence such notes. G From: Herv? Pag?s <hpages.on.github at gmail.com> Sent: Tuesday, February 6, 2024 2:40 PM To: Izmirlian, Grant (NIH/NCI) [E] <izmirlig at mail.nih.gov>; Duncan Murdoch <murdoch.duncan at gmail.com>;
2008 Jan 08
1
use "save.image" inside a function?
...aste("check",t)) # [1] "check 2" As you can see, the value right before I save the workspace is restored. However, if I run the saving inside a function, it seems that it restores to the vale outside the function rather than the one before the save command: rm(list=ls()) test.fcn <- function(t=1){ t = t+1 print(paste("before",t)) save.image("tt.RData") t = t+1 print(paste("after",t)) load("tt.RData") print(paste("check",t)) } t = 1 test.fcn(t=t) # [1]...