Search code examples
rnls

How to retrieve the original data from nls model in R?


non-linear least square (nls() function in core R) generates a list object with a bunch of useful info inside, is there also the original data that were the input of the model? I suspect yes, as functions as predict() or fitted() are actually defined on this object and generate predictions in ranges of the original fitted data. So, how can one access this data through the model object?

Imagine this setting

# generating some data
generated_data <- hist(rnorm(1000, 50, 10))
data <- data.frame(x = generated_data$mids, y = generated_data$counts) 

# a dummy model
my_model <- nls(y ~ N * dnorm(x, m, s), data=data, start=c(m=55, s=12, N=sum(data$y)) ) 

How can I access x and y values by the my_model object?


Solution

  • The data indeed is there but it's a bit buried within the environment of the model that is also saved within the object, I figured it out looking at the source code. You can access the input values of through getEnv() function which is available through the m object and that is part of the list returned by nls.

    So on the dummy example:

    # generating some data
    generated_data <- hist(rnorm(1000, 50, 10))
    data <- data.frame(x = generated_data$mids, y = generated_data$counts) 
    
    # a dummy model
    my_model <- nls(y ~ N * dnorm(x, m, s), data=data, start=c(m=55, s=12, N=sum(data$y)) ) 
    
    original_x_values <- my_model$m$getEnv()$x
    original_y_values <- my_model$m$getEnv()$y