Error In Integration Non-finite Function Value
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up “non-finite function value” when using integrate() in R up vote 0 down vote favorite 1 I am using the following R code, taken from a published paper (citation below). This is the code: int2=function(x,r,n,p) { (1+x)^((n-1-p)/2)*(1+(1-r^2)*x)^(-(n-1)/2)*x^(-3/2)*exp(-n/(2*x))} integrate(f=int2,lower=0,upper=Inf,n=530,r=sqrt(.245),p=3, stop.on.error=FALSE) When I run it, I get the error "non-finite function value". Yet Maple is able to compute this as 4.046018765*10^27. I tried using "integral" in package pracma, which gives me a different error: Error in if (delta < tol) break : missing value where TRUE/FALSE needed The overall goal is to compute a ratio of two integrals, as described in Wetzels & Wagenmakers (2012) "A default Bayesian hypothesis test for correlations" (http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3505519/). The entire function is as follows: jzs.pcorbf = function(r0, r1, p0, p1, n) { int = function(r,n,p,g) { (1+g)^((n-1-p)/2)*(1+(1-r^2)*g)^(-(n-1)/2)*g^(-3/2)*exp(-n/(2*g))}; bf10=integrate(int, lower=0,upper=Inf,r=r1,p=p1,n=n)$value/ integrate(int,lower=0,upper=Inf,r=r0,p=p0,n=n)$value; return(bf10) } Thanks! r share|improve this question asked Feb 22 '15 at 2:03 Dimitri 104 2 missing or NaN input values are considered as non-finite. –Metrics Feb 22 '15 at 2:32 I don't think that is it. There are no missing or NaN input values. There is no data. It's the integration of a simple mathematical expression. –Dimitri Feb 22 '15 at 2:58 2 That's not a good way of looking at it, Dimitri: numerical integration does use "data," as it has to calculate the value of the integrand at a specified number of locations. –Carl Witthoft Feb 22 '15 at 13:30 add a comment| 1 Answer 1 active oldest votes up vote 3 down vote accepted The issue is that your integral function is generating NaN values when called with x values in its domain. You're integrating from 0 to Infinity, so let's check a valid x value of 1000: int2(1000, sqrt(0.245), 53
and 'non-finite function value' error Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] Here is a self-contained example of my problem. set.seed(100) x = rbeta(100, 10.654, 10.439) # So the shape parameters and the exteremes are a = 10.654 b = 10.439 xmax = 1 http://stackoverflow.com/questions/28653672/non-finite-function-value-when-using-integrate-in-r xmin = 0 # Using the non-standardized form (as in my application and this shouldn't make any difference) of the # Beta density function, I specify the integrand (i.e., xf(x)) as integrand = function(x) {x*((1/beta(a,b))*((x^(a - 1)*(xmax - x)^(b-1)) / xmax^(a + b https://stat.ethz.ch/pipermail/r-help/2011-June/281723.html - 1)))} # Say I want to integrate in the range (0, 0.45) and then in (0.45, Inf) # In (0, 0.45) integrate(integrand, lower = 0, upper = 0.45)$val [1] 0.1176079 # In (0.45, Inf) integrate(integrand, lower = 0.45, upper = Inf)$val Error in integrate(integrand, lower = 0.45, upper = Inf) : non-finite function value The same thing happens when I integrate f(x) only. Thanks again. -- View this message in context: http://r.789695.n4.nabble.com/numerical-integration-and-non-finite-function-value-error-tp3618486p3619761.html Sent from the R help mailing list archive at Nabble.com. Previous message: [R] numerical integration and 'non-finite function value' error Next message: [R] numerical integration and 'non-finite function value' error Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] More information about the R-help mailing list
Sign in Pricing Blog Support Search GitHub This repository Watch 9 Star https://github.com/richarddmorey/BayesFactor/issues/22 41 Fork 18 richarddmorey/BayesFactor Code Issues 13 Pull requests 0 Projects 0 Pulse Graphs New issue anovaBF error: Error in integrate(f2... non-finite function value #22 Closed jonathon-love opened this Issue Aug 22, 2014 · 0 comments Projects None yet Labels bug Milestone No milestone error in Assignees richarddmorey 2 participants jonathon-love commented Aug 22, 2014 data <- read.table("ssgo-fred.csv", sep=",", header=TRUE) data$target <- as.factor(data$target) data$difficulty <- as.factor(data$difficulty) data$response <- as.factor(data$response) anovaBF(block ~ target * difficulty + response, data, whichRandom="response") outputs |++++++++++++++++++++++++++++++++++++++++ | 80% Error in integrate(f2, 0, Inf, y = y, Xm = X, rscale error in integration = rscale, gMap = gMap, : non-finite function value |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% Bayes factor analysis -------------- [1] target + response : NA ±NA% [2] difficulty + response : NA ±NA% [3] target + difficulty + response : NA ±NA% [4] target + difficulty + target:difficulty + response : NA ±NA% will email .csv richarddmorey added bug and removed bug labels Aug 22, 2014 richarddmorey self-assigned this Aug 22, 2014 richarddmorey added a commit that referenced this issue Aug 23, 2014 richarddmorey … fixes #22 72c2f61 richarddmorey referenced this issue Aug 23, 2014 Merged Improved integration in unbalanced one-way ANOVA #23 richarddmorey closed this in #23 Aug 23, 2014 Sign up for free to join this conversation on GitHub. Already have an a