Home > Mobile >  How lm function in R only displays call and coefficient of the list but it has got other attributes
How lm function in R only displays call and coefficient of the list but it has got other attributes

Time:11-29

enter image description here

In this picture, lm function only has got call and coefficients, but if using '$', other attributes can show up. How did a function just show part results like this ?

CodePudding user response:

When you enter a, it just runs the print method for the "lm" class on a (since class(a) is "lm"). The print method could be anything at all. There's no requirements on how the printed output corresponds to the underlying data.

Here's the print method called to display the output for an "lm" object

stats:::print.lm
#> function (x, digits = max(3L, getOption("digits") - 3L), ...) 
#> {
#>     cat("\nCall:\n", paste(deparse(x$call), sep = "\n", collapse = "\n"), 
#>         "\n\n", sep = "")
#>     if (length(coef(x))) {
#>         cat("Coefficients:\n")
#>         print.default(format(coef(x), digits = digits), print.gap = 2L, 
#>             quote = FALSE)
#>     }
#>     else cat("No coefficients\n")
#>     cat("\n")
#>     invisible(x)
#> }
#> <bytecode: 0x7f8607193f38>
#> <environment: namespace:stats>

Created on 2021-11-28 by the reprex package (v2.0.1)

But again it could be anything. You can define a class "my_new_class" that just prints "zebra" no matter what, for any object regardless of the underlying data.

a <- lm(mpg ~ cyl, mtcars)
a
#> 
#> Call:
#> lm(formula = mpg ~ cyl, data = mtcars)
#> 
#> Coefficients:
#> (Intercept)          cyl  
#>      37.885       -2.876
class(a)
#> [1] "lm"

print.my_new_class <- function(x) cat('zebra\n')
class(a) <- 'my_new_class'
class(a)
#> [1] "my_new_class"
a
#> zebra

Created on 2021-11-28 by the reprex package (v2.0.1)

If you want to see the full output, you could manually run a different print method. The full output is a lot though, which explains why not all of it is printed by default.

a <- lm(mpg ~ cyl, mtcars)
print.default(a)
#> $coefficients
#> (Intercept)         cyl 
#>    37.88458    -2.87579 
#> 
#> $residuals
#>           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
#>           0.3701643           0.3701643          -3.5814159           0.7701643 
#>   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
#>           3.8217446          -2.5298357          -0.5782554          -1.9814159 
#>            Merc 230            Merc 280           Merc 280C          Merc 450SE 
#>          -3.5814159          -1.4298357          -2.8298357           1.5217446 
#>          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
#>           2.4217446           0.3217446          -4.4782554          -4.4782554 
#>   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
#>          -0.1782554           6.0185841           4.0185841           7.5185841 
#>       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
#>          -4.8814159           0.6217446           0.3217446          -1.5782554 
#>    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
#>           4.3217446           0.9185841          -0.3814159           4.0185841 
#>      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
#>           0.9217446          -0.9298357           0.1217446          -4.9814159 
#> 
#> $effects
#>  (Intercept)          cyl                                                     
#> -113.6497374  -28.5956807   -3.7042540    0.7095969    3.8234479   -2.5904031 
#>                                                                               
#>   -0.5765521   -2.1042540   -3.7042540   -1.4904031   -2.8904031    1.5234479 
#>                                                                               
#>    2.4234479    0.3234479   -4.4765521   -4.4765521   -0.1765521    5.8957460 
#>                                                                               
#>    3.8957460    7.3957460   -5.0042540    0.6234479    0.3234479   -1.5765521 
#>                                                                               
#>    4.3234479    0.7957460   -0.5042540    3.8957460    0.9234479   -0.9904031 
#>                           
#>    0.1234479   -5.1042540 
#> 
#> $rank
#> [1] 2
#> 
#> $fitted.values
#>           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
#>            20.62984            20.62984            26.38142            20.62984 
#>   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
#>            14.87826            20.62984            14.87826            26.38142 
#>            Merc 230            Merc 280           Merc 280C          Merc 450SE 
#>            26.38142            20.62984            20.62984            14.87826 
#>          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
#>            14.87826            14.87826            14.87826            14.87826 
#>   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
#>            14.87826            26.38142            26.38142            26.38142 
#>       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
#>            26.38142            14.87826            14.87826            14.87826 
#>    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
#>            14.87826            26.38142            26.38142            26.38142 
#>      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
#>            14.87826            20.62984            14.87826            26.38142 
#> 
#> $assign
#> [1] 0 1
#> 
#> $qr
#> $qr
#>                     (Intercept)          cyl
#> Mazda RX4            -5.6568542 -35.00178567
#> Mazda RX4 Wag         0.1767767   9.94359090
#> Datsun 710            0.1767767   0.21715832
#> Hornet 4 Drive        0.1767767   0.01602374
#> Hornet Sportabout     0.1767767  -0.18511084
#> Valiant               0.1767767   0.01602374
#> Duster 360            0.1767767  -0.18511084
#> Merc 240D             0.1767767   0.21715832
#> Merc 230              0.1767767   0.21715832
#> Merc 280              0.1767767   0.01602374
#> Merc 280C             0.1767767   0.01602374
#> Merc 450SE            0.1767767  -0.18511084
#> Merc 450SL            0.1767767  -0.18511084
#> Merc 450SLC           0.1767767  -0.18511084
#> Cadillac Fleetwood    0.1767767  -0.18511084
#> Lincoln Continental   0.1767767  -0.18511084
#> Chrysler Imperial     0.1767767  -0.18511084
#> Fiat 128              0.1767767   0.21715832
#> Honda Civic           0.1767767   0.21715832
#> Toyota Corolla        0.1767767   0.21715832
#> Toyota Corona         0.1767767   0.21715832
#> Dodge Challenger      0.1767767  -0.18511084
#> AMC Javelin           0.1767767  -0.18511084
#> Camaro Z28            0.1767767  -0.18511084
#> Pontiac Firebird      0.1767767  -0.18511084
#> Fiat X1-9             0.1767767   0.21715832
#> Porsche 914-2         0.1767767   0.21715832
#> Lotus Europa          0.1767767   0.21715832
#> Ford Pantera L        0.1767767  -0.18511084
#> Ferrari Dino          0.1767767   0.01602374
#> Maserati Bora         0.1767767  -0.18511084
#> Volvo 142E            0.1767767   0.21715832
#> attr(,"assign")
#> [1] 0 1
#> 
#> $qraux
#> [1] 1.176777 1.016024
#> 
#> $pivot
#> [1] 1 2
#> 
#> $tol
#> [1] 1e-07
#> 
#> $rank
#> [1] 2
#> 
#> attr(,"class")
#> [1] "qr"
#> 
#> $df.residual
#> [1] 30
#> 
#> $xlevels
#> named list()
#> 
#> $call
#> lm(formula = mpg ~ cyl, data = mtcars)
#> 
#> $terms
#> mpg ~ cyl
#> attr(,"variables")
#> list(mpg, cyl)
#> attr(,"factors")
#>     cyl
#> mpg   0
#> cyl   1
#> attr(,"term.labels")
#> [1] "cyl"
#> attr(,"order")
#> [1] 1
#> attr(,"intercept")
#> [1] 1
#> attr(,"response")
#> [1] 1
#> attr(,".Environment")
#> <environment: R_GlobalEnv>
#> attr(,"predvars")
#> list(mpg, cyl)
#> attr(,"dataClasses")
#>       mpg       cyl 
#> "numeric" "numeric" 
#> 
#> $model
#>                      mpg cyl
#> Mazda RX4           21.0   6
#> Mazda RX4 Wag       21.0   6
#> Datsun 710          22.8   4
#> Hornet 4 Drive      21.4   6
#> Hornet Sportabout   18.7   8
#> Valiant             18.1   6
#> Duster 360          14.3   8
#> Merc 240D           24.4   4
#> Merc 230            22.8   4
#> Merc 280            19.2   6
#> Merc 280C           17.8   6
#> Merc 450SE          16.4   8
#> Merc 450SL          17.3   8
#> Merc 450SLC         15.2   8
#> Cadillac Fleetwood  10.4   8
#> Lincoln Continental 10.4   8
#> Chrysler Imperial   14.7   8
#> Fiat 128            32.4   4
#> Honda Civic         30.4   4
#> Toyota Corolla      33.9   4
#> Toyota Corona       21.5   4
#> Dodge Challenger    15.5   8
#> AMC Javelin         15.2   8
#> Camaro Z28          13.3   8
#> Pontiac Firebird    19.2   8
#> Fiat X1-9           27.3   4
#> Porsche 914-2       26.0   4
#> Lotus Europa        30.4   4
#> Ford Pantera L      15.8   8
#> Ferrari Dino        19.7   6
#> Maserati Bora       15.0   8
#> Volvo 142E          21.4   4
#> 
#> attr(,"class")
#> [1] "lm"

Created on 2021-11-28 by the reprex package (v2.0.1)

CodePudding user response:

You can use summary to see more of the results of lm.

summary(a)

Example (taken from this example)

#Anscombe's Quartet Q1 Data
y=c(8.04,6.95,7.58,8.81,8.33,9.96,7.24,4.26,10.84,4.82,5.68)
x1=c(10,8,13,9,11,14,6,4,12,7,5)
#Some fake data, set the seed to be reproducible.
set.seed(15)
x2=sqrt(y) rnorm(length(y))
model=lm(y~x1 x2)

summary(model)

Output

Call:
lm(formula = y ~ x1   x2)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.69194 -0.61053 -0.08073  0.60553  1.61689 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept)   0.8278     1.7063   0.485  0.64058   
x1            0.5299     0.1104   4.802  0.00135 **
x2            0.6443     0.4017   1.604  0.14744   
---
Signif. codes:  0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.141 on 8 degrees of freedom
Multiple R-squared:  0.7477,    Adjusted R-squared:  0.6846 
F-statistic: 11.85 on 2 and 8 DF,  p-value: 0.004054
  • Related