README.md

Travis build
status Codecov test
coverage

Overview

lm.hw4 is a package to fit and analyze linear regression model, including following functions.

Installation

install.packages("devtools")
library(devtools)
install_github("leyaozh/lm.hw4", build_vignettes = TRUE, force = TRUE)

Usage

library(lm.hw4)

# use dataset mtcats as an example
attach(mtcars)
x = cbind(hp, wt, hp * wt)
y = mpg

z = lm_fit(x, y, add.intercept = TRUE, method = "qr")
z
#> $add.intercept
#> [1] TRUE
#> 
#> $coefficients
#> (Intercept)          x1          x2          x3 
#> 49.80842343 -0.12010209 -8.21662430  0.02784815 
#> 
#> $residuals
#>  [1] -2.0954741 -0.7813755 -2.7848771  1.3707560  1.4100448 -0.7854161
#>  [7] -1.1074453  2.7411309  1.9500834  0.6462128 -0.7537872  1.2500604
#> [13]  1.0607148 -0.8790873 -1.6217868 -1.4949003  2.1977932  4.5513369
#> [19] -2.2319540  3.6541302 -3.0631732 -2.0744146 -2.7177637 -1.7311118
#> [25]  3.2640401 -2.2390044 -0.7187056  1.8336953  0.4396692  0.1701019
#> [31]  1.4541328 -0.9136259
#> 
#> $effects
#>   (Intercept)            x1            x2            x3               
#> -113.64973741  -26.04559222  -15.89423036    8.07999113    1.51781070 
#>                                                                       
#>   -1.02141990   -0.68576630    2.55781415    1.91580583    0.50988326 
#>                                                                       
#>   -0.89011674    1.12119100    1.07222009   -0.88822537   -1.91166212 
#>                                                                       
#>   -1.69803908    2.21679308    5.31175485   -0.87005072    4.75998225 
#>                                                                       
#>   -2.54554657   -2.12810729   -2.72459486   -1.33991566    3.19524989 
#>                                                                       
#>   -1.22976136    0.06542025    3.04184062    0.97202255    0.56994833 
#>                             
#>    2.35227885   -0.62692830 
#> 
#> $rank
#> [1] 4
#> 
#> $fitted.values
#>  [1] 23.09547 21.78138 25.58488 20.02924 17.28996 18.88542 15.40745 21.65887
#>  [9] 20.84992 18.55379 18.55379 15.14994 16.23929 16.07909 12.02179 11.89490
#> [17] 12.50221 27.84866 32.63195 30.24587 24.56317 17.57441 17.91776 15.03111
#> [25] 15.93596 29.53900 26.71871 28.56630 15.36033 19.52990 13.54587 22.31363
#> 
#> $qr
#> $qr
#>                             hp            wt              
#>  [1,] -5.6568542 -829.78980772 -1.819951e+01 -2.911820e+03
#>  [2,]  0.1767767  381.74189579  3.588740e+00  1.769947e+03
#>  [3,]  0.1767767    0.12620114  4.098743e+00  6.385918e+02
#>  [4,]  0.1767767    0.08166844 -9.210001e-02  2.901446e+02
#>  [5,]  0.1767767   -0.08860368  5.328237e-04  1.269484e-01
#>  [6,]  0.1767767    0.09476629 -1.632227e-01  1.711451e-01
#>  [7,]  0.1767767   -0.27197365  1.276918e-01  4.434795e-02
#>  [8,]  0.1767767    0.20740784 -1.949441e-01  1.154693e-01
#>  [9,]  0.1767767    0.12096200 -1.102864e-01  1.167319e-01
#> [10,]  0.1767767    0.04761401 -1.174894e-01  1.573710e-01
#> [11,]  0.1767767    0.04761401 -1.174894e-01  1.573710e-01
#> [12,]  0.1767767   -0.10170154 -1.418246e-01  6.333227e-02
#> [13,]  0.1767767   -0.10170154 -5.887229e-02  9.608607e-02
#> [14,]  0.1767767   -0.10170154 -7.107116e-02  9.126934e-02
#> [15,]  0.1767767   -0.16719081 -3.729763e-01 -2.209256e-01
#> [16,]  0.1767767   -0.19338652 -3.927318e-01 -3.269106e-01
#> [17,]  0.1767767   -0.23268009 -3.394127e-01 -4.170315e-01
#> [18,]  0.1767767    0.19692956  5.567198e-02 -1.770314e-01
#> [19,]  0.1767767    0.23360355  1.666235e-01 -4.303952e-01
#> [20,]  0.1767767    0.19954913  1.424540e-01 -2.902271e-01
#> [21,]  0.1767767    0.11572286  6.137739e-02 -1.240395e-02
#> [22,]  0.1767767   -0.02311440 -7.572679e-02  1.421396e-01
#> [23,]  0.1767767   -0.02311440 -5.498872e-02  1.415393e-01
#> [24,]  0.1767767   -0.27197365  6.181793e-02 -4.214949e-02
#> [25,]  0.1767767   -0.08860368 -9.827797e-02  9.491211e-02
#> [26,]  0.1767767    0.19692956  1.203260e-01 -2.556231e-01
#> [27,]  0.1767767    0.13144028  1.270521e-01 -9.743888e-02
#> [28,]  0.1767767    0.07380972  3.299583e-01 -9.612300e-02
#> [29,]  0.1767767   -0.32174550  2.684062e-01  1.790572e-01
#> [30,]  0.1767767   -0.08860368  1.639976e-01  1.799467e-01
#> [31,]  0.1767767   -0.50773504  3.319609e-01 -4.863129e-02
#> [32,]  0.1767767    0.08428801  1.176044e-02  6.763697e-02
#> 
#> $rank
#> [1] 4
#> 
#> $qraux
#> [1] 1.176777 1.081668 1.087675 1.132369
#> 
#> $pivot
#> [1] 1 2 3 4
#> 
#> attr(,"class")
#> [1] "qr"
#> 
#> $df.residual
#> [1] 28
#> 
#> $method
#> [1] "qr"
z.summary = lm_summary(z, correlation = TRUE)
z.summary
#> $residuals
#>          1          2          3          4          5          6          7 
#> -2.0954741 -0.7813755 -2.7848771  1.3707560  1.4100448 -0.7854161 -1.1074453 
#>          8          9         10         11         12         13         14 
#>  2.7411309  1.9500834  0.6462128 -0.7537872  1.2500604  1.0607148 -0.8790873 
#>         15         16         17         18         19         20         21 
#> -1.6217868 -1.4949003  2.1977932  4.5513369 -2.2319540  3.6541302 -3.0631732 
#>         22         23         24         25         26         27         28 
#> -2.0744146 -2.7177637 -1.7311118  3.2640401 -2.2390044 -0.7187056  1.8336953 
#>         29         30         31         32 
#>  0.4396692  0.1701019  1.4541328 -0.9136259 
#> 
#> $coefficients
#>                Estimate Std. Error   t value     Pr(>|t|)
#> (Intercept) 49.80842343 3.60515580 13.815887 5.005761e-14
#> x1          -0.12010209 0.02469835 -4.862758 4.036243e-05
#> x2          -8.21662430 1.26970814 -6.471270 5.199287e-07
#> x3           0.02784815 0.00741958  3.753332 8.108307e-04
#> 
#> $aliased
#> (Intercept)          x1          x2          x3 
#>       FALSE       FALSE       FALSE       FALSE 
#> 
#> $sigma
#> [1] 2.152752
#> 
#> $df
#> [1]  4 28  4
#> 
#> $r.squared
#> [1] 0.8847637
#> 
#> $adj.r.squared
#> [1] 0.872417
#> 
#> $fstatistic
#>    value    numdf    dendf 
#> 71.65967  3.00000 28.00000 
#> 
#> $cov.unscaled
#>              (Intercept)            x1           x2            x3
#> (Intercept)  2.804531074 -0.0169996165 -0.945539647  5.366548e-03
#> x1          -0.016999616  0.0001316279  0.005310586 -3.767720e-05
#> x2          -0.945539647  0.0053105864  0.347872412 -1.850732e-03
#> x3           0.005366548 -0.0000376772 -0.001850732  1.187875e-05
#> 
#> $correlation
#>             (Intercept)         x1         x2         x3
#> (Intercept)   1.0000000 -0.8847794 -0.9572814  0.9297784
#> x1           -0.8847794  1.0000000  0.7847991 -0.9528382
#> x2           -0.9572814  0.7847991  1.0000000 -0.9104332
#> x3            0.9297784 -0.9528382 -0.9104332  1.0000000
z.anova = lm_anova(z)
z.anova
#> $Df
#> [1]  3 28
#> 
#> $`Sum Sq`
#> [1] 996.2857 129.7615
#> 
#> $`Mean Sq`
#> [1] 332.095230   4.634339
#> 
#> $`F value`
#> [1] 71.65967
#> 
#> $`Pr(>F)`
#> [1] 2.981394e-13

detach(mtcars)


leyaozh/lm.hw4 documentation built on Dec. 3, 2019, 7:18 a.m.