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>:> 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. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On 29-02-2012, at 14:02, Freddy Hern?ndez wrote:> 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) > }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