Sonuçlar lm () den bir denkleme nasıl çevrilir?


29

lm()Bir değeri tahmin etmek için kullanabiliriz , ancak bazı durumlarda sonuç formülünün denklemine hala ihtiyacımız var. Örneğin, denklemi parsellere ekleyin.


2
Lütfen sorunuzu tekrar yazabilir veya bazı ayrıntılar ekleyebilir misiniz? Genel olarak R lmve lineer modellere oldukça aşinayım , fakat tam olarak ne istediğinizi tam olarak bilmiyorsunuz. Açıklamak için bir örnek veya bir şey verebilir misiniz? Bu bir konu için mi?
Glen_b -Reinstate Monica,

2
I guess you want the coefficients of the linear regression formula. Try calling coef() on the fitted lm object, as in: mod <- lm(y ~ x); coef(mod)
Jake Westfall

If you type lm(y~x)$call it tells you the formula is y ~ x. If you mean something different from that, you need to be more specific.
Glen_b -Reinstate Monica


Yanıtlar:


30

Consider this example:

set.seed(5)            # this line will allow you to run these commands on your
                       # own computer & get *exactly* the same output
x = rnorm(50)
y = rnorm(50)

fit = lm(y~x)
summary(fit)
# Call:
# lm(formula = y ~ x)
# 
# Residuals:
#      Min       1Q   Median       3Q      Max 
# -2.04003 -0.43414 -0.04609  0.50807  2.48728 
# 
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)
# (Intercept) -0.00761    0.11554  -0.066    0.948
# x            0.09156    0.10901   0.840    0.405
# 
# Residual standard error: 0.8155 on 48 degrees of freedom
# Multiple R-squared: 0.01449,  Adjusted R-squared: -0.006046 
# F-statistic: 0.7055 on 1 and 48 DF,  p-value: 0.4051 

The question, I'm guessing, is how to figure out the regression equation from R's summary output. Algebraically, the equation for a simple regression model is:

y^i=β^0+β^1xi+ε^iwhere εN(0, σ^2)
We just need to map the summary.lm() output to these terms. To wit:

  • β^0 is the Estimate value in the (Intercept) row (specifically, -0.00761)
  • β^1 is the Estimate value in the x row (specifically, 0.09156)
  • σ^ is the Residual standard error (specifically, 0.8155)

Plugging these in above yields:

y^i=0.00761 + 0.09156xi + ε^iwhere εN(0, 0.81552)
For a more thorough overview, you may want to read this thread: Interpretation of R's lm() output.


2
Given the OP's mention of a wish to put equations on graphs, I've been pondering whether they actually want a function to take the output of lm and produce a character expression like "y^=0.00761+0.09156x" suitable for such a plotting task (hence my repeated call to clarify what they wanted - which hasn't been done, unfortunately).
Glen_b -Reinstate Monica

6

If what you want is to predict scores using your resulting regression equation, you can construct the equation by hand by typing summary(fit) (if your regression analysis is stored in a variable called fit, for example), and looking at the estimates for each coefficient included in your model.

For example, if you have a simple regression of the type y=β0+β1x+ϵ, and you get an estimate of the intercept (β0) of +0.5 and an estimate of the effect of x on y (β1) of +1.6, you would predict an individual's y score from their x score by computing: y^=0.5+1.6x.

However, this is the hard route. R has a built-in function, predict(), which you can use to automatically compute predicted values given a model for any dataset. For example: predict(fit, newdata=data), if the x scores you want to use to predict y scores are stored in the variable data. (Note that in order to see the predicted scores for the sample on which your regression was performed, you can simply type fit$fitted or fitted(fit); these will give you the predicted, a.k.a. fitted, values.)


0

If you want to show the equation, like to cut/paste into a doc, but don't want to fuss with putting the entire equation together:

R> library(MASS)
R> crime.lm <- lm(y~., UScrime)
R> cc <- crime.lm$coefficients
R> (eqn <- paste("Y =", paste(round(cc[1],2), paste(round(cc[-1],2), names(cc[-1]), sep=" * ", collapse=" + "), sep=" + "), "+ e"))
[1] "Y = -5984.29 + 8.78 * M + -3.8 * So + 18.83 * Ed + 19.28 * Po1 + -10.94 * Po2 + -0.66 * LF + 1.74 * M.F + -0.73 * Pop + 0.42 * NW + -5.83 * U1 + 16.78 * U2 + 0.96 * GDP + 7.07 * Ineq + -4855.27 * Prob + -3.48 * Time + e"

0

Building on keithpjolley's answer, this replaces the '+' signs used in the separator with the actual sign of the co-efficient.

modelcrime <- lm(y~., UScrime)
modelcrime_coeff <- modelcrime$coefficients
modelcrime_coeff_sign <- sign(modelcrime_coeff)
modelcrime_coeff_prefix <- case_when(modelcrime_coeff_sign == -1 ~ " - ",
                                     modelcrime_coeff_sign == 1 ~ " + ",
                                     modelcrime_coeff_sign == 0 ~ " + ")
modelcrime_eqn <- paste("y =", paste(if_else(modelcrime_coeff[1]<0, "- ", ""),
                                         abs(round(modelcrime_coeff[1],3)),
                                     paste(modelcrime_coeff_prefix[-1],
                                           abs(round(modelcrime_coeff[-1],3)),
                                           " * ",
                                           names(modelcrime_coeff[-1]),
                                           sep = "", collapse = ""),
                                     sep = ""))
modelcrime_eqn

produces the result

[1] "y = - 5984.288 + 8.783 * M - 3.803 * So + 18.832 * Ed + 19.28 * Po1 - 10.942 * Po2 - 0.664 * LF + 1.741 * M.F - 0.733 * Pop + 0.42 * NW - 5.827 * U1 + 16.78 * U2 + 0.962 * GDP + 7.067 * Ineq - 4855.266 * Prob - 3.479 * Time"
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.