I am trying to find the optimal "lambda" parameter for the Box-Cox transformation.
I am using the implementation from the MASS package, so I only need to create the model and extract the lambda.
Here is the code for the function:
library(MASS)
find_lambda <- function(x) {
# Function to find the best lambda for the Box-Cox transform
my_tmp <- data.frame(x = x) # Create a temporary data frame, to use it with the lm
str(my_tmp) # Gives the expected output
the_lm <- lm(x ~ 1, data = my_tmp) # Creates the linear model, no error here
print(summary(the_lm)) # Prints the summary, as expected
out <- boxcox(the_lm, plotit=FALSE) # Gives the error
best_lambda <- out$x[which.max(out$y)] # Extracting the best fitting lambda
return(best_lambda)
}
find_lambda(runif(100))
It gives the following error:
Error in is.data.frame(data) : object 'my_tmp' not found
The interesting thing is that the very same code is working outside the function. In other words, for some reason, the boxcox function from the MASS package is looking for the variable in the global environment.
I don't really understand, what exactly is going on... Do you have any ideas?
P.S. I do not provide a software/hardware specification, since this error was sucessfully replicated on a number of my friends' laptops.
P.P.S. I have found the way to solve the initial problem in the forecast package, but I still would like to know, why this code is not working.
lm (formula, data, …) The following example shows how to use this function in R to do the following: The following code shows how to use the lm () function to fit a linear regression model in R: We can then use the summary () function to view the summary of the regression model fit:
A step-by-step guide to linear regression in R. 1 Step 1: Load the data into R. Follow these four steps for each dataset: 2 Step 2: Make sure your data meet the assumptions. 3 Step 3: Perform the linear regression analysis. 4 Step 4: Check for homoscedasticity. 5 Step 5: Visualize the results with a graph. More items
Types of Linear Model in R 1 Simple Linear Regression#N#This model helps us to explain a relationship between one dependent variable and one... 2 Multiple Linear Regression More ...
Add the regression line using geom_smooth () and typing in lm as your method for creating the line. This will add the line of the linear regression as well as the standard error of the estimate (in this case +/- 0.01) as a light grey stripe surrounding the line:
Sometimes user contributed packages don't always do a great job tracking the environments where calls were executed when manipulating functions calls. The quickest fix for you would be to change the line from
the_lm <- lm(x ~ 1, data = my_tmp)
to
the_lm <- lm(x ~ 1, data = my_tmp, y=True, qr=True)
Because if the y
and qr
are not requested from the lm
call, the boxcox
function tries to re-run lm
with those parameters via an update
call and things get mucked up inside a function scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With