Search code examples
rregressionlmpredictcoefficients

R model variable score


I built a linear regression model in R and tried to get the contribution amount (not the coefficient) of each of the explanatory variables (independent variables) (i.e. x1, x2, x3). Question:

  1. How can I get such amount for the fitted data. That is numeric value contributed by each x (x1, x2, x3) for each observation.
  2. How can I get the same amount for new (unseen) data
df = data.frame(y, x1, x2, x3)
mod = lm(y ~ x1 + x2 + x3, data = df)

Solution

  • As @Ali suggested, I think that summary(mod) does answer your question. Let me give a bit more explanation. Since you do not provide your data, I will use the built-in iris data as an example.

    mod = lm(Sepal.Length ~ ., data=iris[,1:4])
    summary(mod)$coefficients
                   Estimate Std. Error   t value     Pr(>|t|)
    (Intercept)   1.8559975 0.25077711  7.400984 9.853855e-12
    Sepal.Width   0.6508372 0.06664739  9.765380 1.199846e-17
    Petal.Length  0.7091320 0.05671929 12.502483 7.656980e-25
    Petal.Width  -0.5564827 0.12754795 -4.362929 2.412876e-05
    

    Notice the column labeled "Estimate". Those are the model coefficients. Just to be really explicit, let's go through an an example of how they relate to the prediction.

    iris[1,]
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    1          5.1         3.5          1.4         0.2  setosa
    
    predict(mod, newdata=iris[1,])
           1 
    5.015416 
    

    OK, so if we predict the first row using the model, we get the answer 5.015416. How did that come from the coefficients?

     1.8559975 +            ## Intercept
     0.6508372 * 3.5 +      ## Sepal.Width
     0.7091320 * 1.4 +      ## Petal.Length
    -0.5564827 * 0.2        ## Petal.Width
    [1] 5.015416