Kamil S Jaron
Kamil S Jaron

Reputation: 546

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?

Upvotes: 2

Views: 600

Answers (2)

bobloblawlawblog
bobloblawlawblog

Reputation: 86

Broom has nice features for dealing with many models. You can use broom::augment() with added benefits :)

If you don't want to use broom, then Kamil's answer works.

library(dplyr)
library(broom)
my_model %>% augment()
# A tibble: 13 × 4
      x     y .fitted  .resid
  <dbl> <int>   <dbl>   <dbl>
1  22.5     6    4.27   1.73 
2  27.5    18   15.1    2.88 
3  32.5    45   41.6    3.36 
4  37.5    94   89.1    4.87 
5  42.5   137  148.   -11.3  
6  47.5   191  192.    -0.740
# … with 7 more rows

Upvotes: 3

Kamil S Jaron
Kamil S Jaron

Reputation: 546

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

Upvotes: 3

Related Questions