[R] Getting an error calling MASS::boxcox in a function

Ron Crump r@e@crump @end|ng |rom w@rw|ck@@c@uk
Sat Jul 8 13:28:31 CEST 2023


Firstly, apologies as I have posted this on community.rstudio.com too. 

I want to optimise a Box-Cox transformation on columns of a matrix (ie, a unique lambda for each column). So I wrote a function that includes the call to MASS::boxcox in order that it can be applied to each column easily. Except that I'm getting an error when calling the function. If I just extract a column of the matrix and run the code not in the function, it works. If I call the function either with an extracted column (ie dd1 in the reprex below) or in a call to apply I get an error (see the reprex below).

I'm sure I'm doing something silly, but I can't see what it is. Any help appreciated.


# Find optimised Lambda for Boc-Cox transformation
BoxCoxLambda <- function(z){
    b <- boxcox(lm(z+1 ~ 1), lambda = seq(-5, 5, length.out = 61), plotit = FALSE)
    b$x[which.max(b$y)]    # best lambda

mrow <- 500
mcol <- 2
dd <- matrix(rgamma(mrow*mcol, shape = 2, scale = 5), nrow = mrow, ncol = mcol)

# Try it not using the BoxCoxLambda function:
dd1 <- dd[,1] # 1st column of dd
bb <- boxcox(lm(dd1+1 ~ 1), lambda = seq(-5, 5, length.out = 101), plotit = FALSE)
print(paste0("1st column's lambda is ", bb$x[which.max(bb$y)]))
#> [1] "1st column's lambda is 0.2"

# Calculate lambda for each column of dd
lambdas <- apply(dd, 2, BoxCoxLambda, simplify = TRUE)
#> Error in eval(predvars, data, env): object 'z' not found

Created on 2023-07-08 with reprex v2.0.2

Thanks for your time and help.

	[[alternative HTML version deleted]]

More information about the R-help mailing list