Skip to content

Converting a function from Splus to R

6 messages · Freddy Hernández, Berend Hasselman, Brian Ripley +2 more

#
I have a function written for Splus, when I run it in R I obtain get an error
because the function has the elements "0.d0" and "2.d0". How can I change it
to run in R?

The function can be found in page 230 from
 http://www.stat.wisc.edu/~mchung/teaching/stat471/stat_computing.pdf

Function is as follows:

gauher <- function(n) {# Gauss-Hermite: returns x,w so that
#\int_-\infty^\infty exp(-x^2) f(x) dx \doteq \sum w_i f(x_i)
EPS <- 3.e-14
PIM4 <- .7511255444649425D0
MAXIT <- 10
m <- trunc((n+1)/2)
x <- w <- rep(-1,n)
for (i in 1:m) {
if (i==1) {
z <- sqrt(2*n+1)-1.85575*(2*n+1)^(-.16667)
} else if(i==2) {
z <- z-1.14*n^.426/z
} else if (i==3) {
z <- 1.86*z-.86*x[1]
} else if (i==4) {
z <- 1.91*z-.91*x[2]
} else {
z <- 2.*z-x[i-2]
}
for (its in 1:MAXIT) {
p1 <- PIM4
p2 <- 0.d0
for (j in 1:n) {
p3 <- p2
p2 <- p1
p1 <- z*sqrt(2.d0/j)*p2-sqrt((j-1)/j)*p3
}
pp <- sqrt(2.d0*n)*p2
z1 <- z
z <- z1-p1/pp
if(abs(z-z1) <= EPS) break
}
x[i] <- z
x[n+1-i] <- -z
w[i] <- 2/(pp*pp)
w[n+1-i] <- w[i]
}
list(x=x,w=w)
}

--
View this message in context: http://r.789695.n4.nabble.com/Converting-a-function-from-Splus-to-R-tp4431416p4431416.html
Sent from the R help mailing list archive at Nabble.com.
#
Change the name to something syntactically valid? The problem is that
you can't (well, you can, but it's ill advised) have variable names
beginning with numbers.  They don't seem to be used much so there
won't be much trouble in that.

Michael

2012/2/29 Freddy Hern?ndez <fhernanb at gmail.com>:
#
On 29-02-2012, at 14:02, Freddy Hern?ndez wrote:

            
I know nothing of S.
But why don't you just try   "0.e0" and "2.e0" or better 0 and 2?
I'm guessing that these are numbers.

Berend
#
On 29/02/2012 13:24, R. Michael Weylandt wrote:
I think that 2.0d0 is someone translating C into R. Just use 2 .  In R, 
2 is a double precision floating-point constant: in C and (recent 
S-PLUS) it is integer and you need to use 2.0 .

  
    
#
Oh...that does make more sense -- seemed like a rather odd choice of
variable name.

Michael

On Wed, Feb 29, 2012 at 8:40 AM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
#
1.0d+0 is Fortran (not C) for a double precision value,
1.0 * 10^0.

1.0e+0 is Fortran for a single precision value, 1.0 * 10^0
and C for a double precision value.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com