similar to: how to pass a function to a function inside a function

Displaying 20 results from an estimated 4000 matches similar to: "how to pass a function to a function inside a function"

2007 Jun 28
2
minor flaw in integrate()
Hi, I noticed a minor flaw in integrate() from package stats: Taking up arguments lower and upper from integrate(), if (lower == Inf) && (upper == Inf) or if (lower == -Inf) && (upper == -Inf) integrate() calculates the value for (lower==-Inf) && (upper==Inf). Rather, it should return 0. Quick fix: ### old code ### ### [snip] else {
2004 Jul 08
1
(PR#7070)
> version _ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 1 minor 7.1 year 2003 month 06 day 16 language R Bug: integrate(f,lower,upper,extra_args) where f <- function(x,extra_args) { body } integrate doesn't pass the extra arguments when calling f. As a first check of this finding I integrated dnorm from
2013 Jul 16
2
Problem following an R bug fix to integrate()
I have been told by the CRAN administrators that the following code generated an error on 64-bit Fedora Linux (gcc, clang) and on Solaris machines (sparc, x86), but runs well on all other systems): > fn <- function(x, y) ifelse(x^2 + y^2 <= 1, 1 - x^2 - y^2, 0) > tol <- 1.5e-8 > fy <- function(x) integrate(function(y) fn(x, y), 0, 1,
2009 Dec 18
1
Numerical Integration
Dear @ll. I have to calculate numerical integrals for triangular and trapezoidal figures. I know you can calculate the exactly, but I want to do it this way to learn how to proceed with more complicated shapes. The code I'm using is the following: integrand<-function(x) { print(x) if(x<fx[1]) return(0) if(x>=fx[1] && x<fx[2]) return((x-fx[1])/(fx[2]-fx[1]))
2004 Jul 08
0
a not-a-bug report on integrate (PR#7070)
Rembert.DeBlander@econ.kuleuven.ac.be wrote: >>version > PLEASE: a) Use a sensible subject line b) Please post only post bug reports if you are sure it is a bug (and here it is not a bug!) c) Check the recent R version (1.9.1) if posting bugs, not an outdated one! d) Only named arguments are passed through the ... argument: integrate(dnorm, lower=-Inf, upper=3, mean=-1, sd=7) Uwe
2019 Apr 12
2
integrate over an infinite region produces wrong results depending on scaling
Dear all, This is the first time I am posting to the r-devel list. On StackOverflow, they suggested that the strange behaviour of integrate() was more bug-like. I am providing a short version of the question (full one with plots: https://stackoverflow.com/q/55639401). Suppose one wants integrate a function that is just a product of two density functions (like gamma). The support of the
2011 Nov 10
2
performance of adaptIntegrate vs. integrate
Dear list, [cross-posting from Stack Overflow where this question has remained unanswered for two weeks] I'd like to perform a numerical integration in one dimension, I = int_a^b f(x) dx where the integrand f: x in IR -> f(x) in IR^p is vector-valued. integrate() only allows scalar integrands, thus I would need to call it many (p=200 typically) times, which sounds suboptimal. The
2007 Feb 27
1
Additional args to fun in integrate() not found?
Hello, fellow Rdicts, I have the code for the program below. I need to integrate a function of "x" and "p". I use integrate to integrate over "x" and pass "p" as an additional argument. "p" is specified and given default value in the argument list. Still, integrate() cannot read "p", unless I explicitly insert a numeric value in the
2012 Apr 26
2
ErrError in f(x, ...) : object 'g.' not found
Hi , R is a new language for me so sorry in advance if this error is to basic for posting. I have tried the R manual and search online for quite a few, if anyone could help i would be very thankful. Here is my code. kappa = 1.1 theta = 0.1 sigma = 0.4 rho = -0.6 v0 = 0.2 r = 0.05 T = 0.5 s0 = 1 K = 0.5 type = 1 Hestoncall = function(kappa,theta,sigma,rho,v0,r,T,s0,K,type) { u = 0.5 b
2011 Aug 03
1
Case-by-case tolerance needed for successful integrate()
Hello, We are trying to use R to simulate a model based on parameters 'a' and 'b'. This involves the following integration: model<-function(s,x,a,b)(exp(-s*x*10^-5.5)*(s^(a-1)*(1-s)^(b-1))) g<- function(x,a,b){ out<-c() for (i in 1:length(x)){ out[i]<-1- (integrate(model,0,1,x[i],a,b)$value / beta(a,b)) } out } x<-
2012 Mar 29
1
Error, Variable is Missing
Hi, I am writing a function to plot a pdf of a distribution, GNL.pdf.fn = function(x,mu,sigma,alpha,beta,rho) { y = x-rho*mu cf.fn = function(s){ cplex = complex(1,0,1) temp1 = alpha*beta*exp(-sigma*s^2/2) temp2 = (alpha-cplex*s)*(beta+cplex*s) out = (temp1/temp2)^rho out } temp.fn = function(s){ (Mod(cf.fn(s)))*cos(Arg(cf.fn(s))-s*y) } int.fn =
2011 Jan 12
1
Integrate and subdivisions limit
Dear all, I have some issues with integrate in R thus I would like to request your help. I am trying to calculate the integral of f(x)*g(x). The f(x) is a step function while g(x) is a polynomial. If f(x) (step function) changes its value only few times (5 or 6 'steps') everything is calulated ok(verified results in scrap paper) but if f(x) takes like 800 different values I receive the
2005 Jan 29
2
Name conflicts when passing arguments for one function to another
I am fairly new to R. I find it surprising that f <- function(x,a) {x-a} uniroot(f, c(0,1), a=.5) works, but integrate(f, 0, 1, a=.5) gives an error: Error in integrate(f, 0, 1, a = 0.5) : argument 4 matches multiple formal arguments What is the best way of avoiding such surprises? Is there a way of telling integrate() that the 'a' argument is for f()? If I wrote my own function
2010 Nov 17
2
Numerical integration
Hi! I was wondering if there are any other functions for numerical integration, besides 'integrate' from the stats package, but which wouldn't require the integrand to be vectorized. Oh, and must be capable of integrating over (-inf,+inf). Thanks in advance, Eduardo Horta [[alternative HTML version deleted]]
2012 Apr 18
0
Numerical integration again
Hi all, Here is an integration function require(pracma) # for 'quadinf' myint=function(j) { quadinf(function(x) (1/(1+exp(-x)))^j*(1-1/(1+exp(-x)))^(k-j)*dnorm(x,mu,casigma),-Inf,Inf) } in any optimization routine. It works fine most of the time but failed with some particular sets of values, say one of the following: k=20 mu=-1.978295 casigma=0.008326927 >
2009 Jan 05
1
transform R to C
Dear R users, i would like to transform the following function from R-code to C-code and call it from R in order to speed up the computation because in my other functions this function is called many times. `dgcpois` <- function(z, lambda1, lambda2) { `f1` <- function(alpha, lambda1, lambda2) return(exp(log(lambda1) * (alpha - 1) - lambda2 * lgamma(alpha))) `f2` <-
2011 Oct 19
1
Sparse covariance estimation (via glasso) shrinking to a "nonzero" constant
I've only been using R on and off for 9 months and started using the glasso package for sparse covariance estimation. I know the concept is to shrink some of the elements of the covariance matrix to zero. However, say I have a dataset that I know has some underlying "baseline" covariance/correlation (say, a value of 0.3), how can I change or incorporate that into to the
2001 Nov 20
1
warning message
Hello everybody. I would like to pose a question regarding a warning message after running a function of my own. The function I created for computing the p-value of the dw statistic has a command line like this: »dwf0 <- function(dw,eigen) { carfun <- function(x) { (prod(1+2*(eigen-dw)*1i*x)^(-1/2)-prod(1-2*(eigen-dw)*1i*x)^(-1/2))/(1i*x) } ; 1/2+integrate(f=carfun,lower = 0,upper =
2005 Sep 16
4
Integrate functions with loops
Hi i am having a problem with the 'integrate' function the function i want to integrate has the form sum(vector^x) i have defined the function with a for loop first - integrandtotest <- function(x) {a<-rep(0,len=2) for (i in 1:2) {a[i]<-t[i]^x} sum(a) } the results gives errors ########### Error in integrate(integrandtotest, lower = 0.1, upper =
2011 Nov 23
2
How to increase precision to handle very low P-values
Hello, Rlisters I have to compute p-values that are on the tail of the distribution, P-values < 10^-20. However, my current implementations enable one to estimate P-values up to 10^-12, or so. A typical example is found below, where t is my critical value. ########### example - code adapted from Rassoc ####################### rho01 = 0.5 rho105 = 0.5 rho005 = 0.5 t = 8 z = 2