Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Passing linear model to another function inside a function

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.

like image 814
R-seny Avatar asked Sep 27 '16 15:09

R-seny


People also ask

How do I use the LM () function in R?

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:

How to do a linear regression in R?

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

What are the different types of linear model in R?

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 ...

How do I add a line to a linear regression model?

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:


1 Answers

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.

like image 73
MrFlick Avatar answered Nov 15 '22 07:11

MrFlick