-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Prof Brian
Ripley
Sent: Wednesday, February 29, 2012 5:41 AM
To: R. Michael Weylandt
Cc: r-help at r-project.org; Freddy Hern?ndez
Subject: Re: [R] Converting a function from Splus to R
On 29/02/2012 13:24, R. Michael Weylandt wrote:
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.
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 .
Michael
2012/2/29 Freddy Hern?ndez<fhernanb at gmail.com>:
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-
Sent from the R help mailing list archive at Nabble.com.