[R] for loop not working in function

From: Dale Steele <Dale_Steele_at_brown.edu>
Date: Wed 11 Oct 2006 - 04:28:21 GMT

I'm trying to write a small function (below) to compute Box & Cox transformations of x for arbitrary values of lambda. I'd like to specify a range of values for lamba (min,max,step) and am having trouble getting the for loop to work. Suggestions?

Any pointers to resources for learning to write functions in R for neophyte programmers? Thanks. --Dale

boxcox <- function(x,min,max,step) {
lambda <- seq(min,max,step)
s <- length(lambda)
for (lambda in 1:s)
n <- nrow(x)
if(lambda ==0) xL <- log(x) else
xL <- ((x^lambda) - 1)/lambda
xLbar <- mean(xL)
t1 <- (-n/2)* log((1/n)*sum((xL - xLbar)^2)) t2 <- (lambda - 1)*sum(log(x))
l= t1 + t2


R-help@stat.math.ethz.ch 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. Received on Wed Oct 11 14:31:49 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 11 Oct 2006 - 08:30:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.