JD1
JD1

Reputation: 27

Determine dataset that was used to generate an lm() model

Lets say I was provided a multiple linear regression model such as:
y = B0 + B1x1 + B2x2

And given the intercept/coef values of this model:
B0 = 0.005275169B1
B1 = 0.067347131
B2 = -0.207909721

BUT.. I don't have the original dataset (x/y values) that generated that model. Is there anyway to generate a new dataset, then feed it into lm() where resulting lm()$coef call would then spit out the same BO, B1, B2 values noted above? In summary, I want to generate a dataset that when fed into lm() produces a model with 100% exact same coef as above.

Upvotes: 0

Views: 252

Answers (2)

dww
dww

Reputation: 31452

I think you are looking for the model field in the lm object. Here's a reproducible example

fit <- lm(mpg ~ disp, data = mtcars)    
newdata <- fit$model
#                      mpg  disp
# Mazda RX4           21.0 160.0
# Mazda RX4 Wag       21.0 160.0
# Datsun 710          22.8 108.0
# Hornet 4 Drive      21.4 258.0
# Hornet Sportabout   18.7 360.0
# Valiant             18.1 225.0
# ...

Upvotes: 0

Maurits Evers
Maurits Evers

Reputation: 50668

To expand on my comment above, here is an example using the mtcars dataset, where we fit a linear model of the form mpg = beta0 + beta1 * disp.

fit <- lm(mpg ~ disp, data = mtcars)
summary(fit)
#
#Call:
#lm(formula = mpg ~ disp, data = mtcars)
#
#Residuals:
#    Min      1Q  Median      3Q     Max
#-4.8922 -2.2022 -0.9631  1.6272  7.2305
#
#Coefficients:
#             Estimate Std. Error t value Pr(>|t|)
#(Intercept) 29.599855   1.229720  24.070  < 2e-16 ***
#disp        -0.041215   0.004712  -8.747 9.38e-10 ***
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
#Residual standard error: 3.251 on 30 degrees of freedom
#Multiple R-squared:  0.7183,   Adjusted R-squared:  0.709
#F-statistic: 76.51 on 1 and 30 DF,  p-value: 9.38e-10

We generate some new data for disp and use the model coefficients to predict a response for mpg.

df <- data.frame(disp = seq(1, 1000, length.out = 20))
df$mpg <- predict(fit, newdata = df)

We now fit the same model to the new data.

fit.new <- lm(mpg ~ disp, data = df)
#
#Call:
#lm(formula = mpg ~ disp, data = df)
#
#Residuals:
#       Min         1Q     Median         3Q        Max
#-1.720e-14 -3.095e-15  1.302e-15  3.618e-15  5.719e-15
#
#Coefficients:
#              Estimate Std. Error    t value Pr(>|t|)
#(Intercept)  2.960e+01  2.235e-15  1.325e+16   <2e-16 ***
#disp        -4.122e-02  3.819e-18 -1.079e+16   <2e-16 ***
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
#Residual standard error: 5.178e-15 on 18 degrees of freedom
#Multiple R-squared:      1,    Adjusted R-squared:      1
#F-statistic: 1.165e+32 on 1 and 18 DF,  p-value: < 2.2e-16
#
#Warning message:
#In summary.lm(fit.new) : essentially perfect fit: summary may be unreliable

Notice how estimates are identical (but standard deviations and t statistics are not!). Also notice the warning at the bottom of the second model fit.


Update

If you have coefficients beta0 and beta1 simply calculate the response as

beta0 <- coef(fit)[1]
beta1 <- coef(fit)[2]
df <- data.frame(disp = seq(1, 1000, length.out = 20))
df$mpg <- beta0 + df$disp * beta1

Upvotes: 1

Related Questions