redcurry
redcurry

Reputation: 2497

Difference running Rscript vs source with ape library

I have the following script in a file (call it "temp.R"):

library(ape)
tree <- rbdtree(0.5, 0.05, 5)
bd.time(tree, 0, 0)

When I run Rscript temp.R I get some results:

$par
    birth     death 
0.5289875 0.0000000 

$SS
[1] 9.21573

$convergence
[1] 0

$iterations
[1] 6

$evaluations
function gradient 
       8       16 

$message
[1] "relative convergence (4)"

However, when I run R and then do:

source('temp.R')

I get the following error:

Error in integrate(Pi, 0, Tmax) : non-finite function value

Does anyone have any idea why there's a difference between Rscript and source such that one works and the other fails? In case it helps, here's the output by running version in R:

               _                           
platform       x86_64-apple-darwin10.8.0   
arch           x86_64                      
os             darwin10.8.0                
system         x86_64, darwin10.8.0        
status                                     
major          3                           
minor          0.1                         
year           2013                        
month          05                          
day            16                          
svn rev        62743                       
language       R                           
version.string R version 3.0.1 (2013-05-16)
nickname       Good Sport

UPDATE: When I run Rscript temp.R several times I sometimes get a similar error message as when running source:

Error in integrate(Pi, 0, Tmax) : non-finite function value
Calls: bd.time ... objective -> CDF.birth.death -> .CDF.birth.death2 -> integrate
Execution halted

Upvotes: 0

Views: 141

Answers (1)

fmic_
fmic_

Reputation: 2446

This is not related to a difference between Rscript and source() it's just that you are using a function that relies on a random process and depending on the starting point it works or it doesn't. I haven't looked carefully enough to what you are trying to achieve specifically, but you may need to change the values you use for the bd.time function.

## works
set.seed(123)
library(ape)
tree <- rbdtree(0.5, 0.05, 5)
bd.time(tree, 0, 0)

## doesn't work
set.seed(12345)
library(ape)
tree <- rbdtree(0.5, 0.05, 5)
bd.time(tree, 0, 0)

Upvotes: 1

Related Questions