library(datasets)
data(iris)
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
data(iris)
summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 
plot(iris)

cor(iris$Petal.Length, iris$Petal.Width)
## [1] 0.9628654
fit <- lm(iris$Petal.Length ~ iris$Petal.Width)
fit
## 
## Call:
## lm(formula = iris$Petal.Length ~ iris$Petal.Width)
## 
## Coefficients:
##      (Intercept)  iris$Petal.Width  
##            1.084             2.230
par(mfrow=c(2,2))
plot(fit)

residuals(fit)
##            1            2            3            4            5 
## -0.129546132 -0.129546132 -0.229546132 -0.029546132 -0.129546132 
##            6            7            8            9           10 
## -0.275534231 -0.352540181 -0.029546132 -0.129546132  0.193447918 
##           11           12           13           14           15 
## -0.029546132  0.070453868  0.093447918 -0.206552082 -0.329546132 
##           16           17           18           19           20 
## -0.475534231 -0.675534231 -0.352540181 -0.052540181 -0.252540181 
##           21           22           23           24           25 
##  0.170453868 -0.475534231 -0.529546132 -0.498528280  0.370453868 
##           26           27           28           29           30 
##  0.070453868 -0.375534231 -0.029546132 -0.129546132  0.070453868 
##           31           32           33           34           35 
##  0.070453868 -0.475534231  0.193447918 -0.129546132 -0.029546132 
##           36           37           38           39           40 
## -0.329546132 -0.229546132  0.093447918 -0.229546132 -0.029546132 
##           41           42           43           44           45 
## -0.452540181 -0.452540181 -0.229546132 -0.821522330 -0.075534231 
##           46           47           48           49           50 
## -0.352540181  0.070453868 -0.129546132 -0.029546132 -0.129546132 
##           51           52           53           54           55 
##  0.494525274  0.071531224  0.471531224  0.017519323  0.171531224 
##           56           57           58           59           60 
##  0.517519323  0.048537175 -0.013498528  0.617519323 -0.305474726 
##           61           62           63           64           65 
##  0.186501472 -0.228468776  0.686501472  0.494525274 -0.382480677 
##           66           67           68           69           70 
##  0.194525274  0.071531224  0.786501472  0.071531224  0.363507423 
##           71           72           73           74           75 
## -0.297450924  0.017519323  0.471531224  0.940513373  0.317519323 
##           76           77           78           79           80 
##  0.194525274  0.594525274  0.125543125  0.071531224  0.186501472 
##           81           82           83           84           85 
##  0.263507423  0.386501472  0.140513373  0.448537175  0.071531224 
##           86           87           88           89           90 
## -0.151462825  0.271531224  0.417519323  0.117519323  0.017519323 
##           91           92           93           94           95 
##  0.640513373  0.394525274  0.240513373 -0.013498528  0.217519323 
##           96           97           98           99          100 
##  0.440513373  0.217519323  0.317519323 -0.536492577  0.117519323 
##          101          102          103          104          105 
## -0.658409271 -0.220444974  0.133566927  0.502549076 -0.189427122 
##          106          107          108          109          110 
##  0.833566927 -0.374456875  1.202549076  0.702549076 -0.558409271 
##          111          112          113          114          115 
## -0.443439023 -0.020444974 -0.266433073 -0.543439023 -1.335415221 
##          116          117          118          119          120 
## -0.912421172  0.402549076  0.710572878  0.687578828  0.571531224 
##          121          122          123          124          125 
## -0.512421172 -0.643439023  1.156560977 -0.197450924 -0.066433073 
##          126          127          128          129          130 
##  0.902549076 -0.297450924 -0.197450924 -0.166433073  1.148537175 
##          131          132          133          134          135 
##  0.779555026  0.856560977 -0.389427122  0.671531224  1.394525274 
##          136          137          138          139          140 
## -0.112421172 -0.835415221  0.402549076 -0.297450924 -0.366433073 
##          141          142          143          144          145 
## -0.835415221 -1.112421172 -0.220444974 -0.312421172 -0.958409271 
##          146          147          148          149          150 
## -1.012421172 -0.320444974 -0.343439023 -0.812421172  0.002549076
summary(fit)
## 
## Call:
## lm(formula = iris$Petal.Length ~ iris$Petal.Width)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.33542 -0.30347 -0.02955  0.25776  1.39453 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       1.08356    0.07297   14.85   <2e-16 ***
## iris$Petal.Width  2.22994    0.05140   43.39   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4782 on 148 degrees of freedom
## Multiple R-squared:  0.9271, Adjusted R-squared:  0.9266 
## F-statistic:  1882 on 1 and 148 DF,  p-value: < 2.2e-16
data2<- subset(iris, iris$Species!="setosa")
plot(data2)

cor(data2$Petal.Length,data2$Petal.Width)
## [1] 0.8233476
fit <- lm(data2$Petal.Length ~ data2$Petal.Width)
summary(fit)
## 
## Call:
## lm(formula = data2$Petal.Length ~ data2$Petal.Width)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.9842 -0.3043 -0.1043  0.2407  1.2755 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         2.2240     0.1926   11.55   <2e-16 ***
## data2$Petal.Width   1.6003     0.1114   14.36   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4709 on 98 degrees of freedom
## Multiple R-squared:  0.6779, Adjusted R-squared:  0.6746 
## F-statistic: 206.3 on 1 and 98 DF,  p-value: < 2.2e-16
library(mlbench)
data(BostonHousing)
dim(BostonHousing)
## [1] 506  14
str(BostonHousing)
## 'data.frame':    506 obs. of  14 variables:
##  $ crim   : num  0.00632 0.02731 0.02729 0.03237 0.06905 ...
##  $ zn     : num  18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
##  $ indus  : num  2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
##  $ chas   : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
##  $ nox    : num  0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
##  $ rm     : num  6.58 6.42 7.18 7 7.15 ...
##  $ age    : num  65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
##  $ dis    : num  4.09 4.97 4.97 6.06 6.06 ...
##  $ rad    : num  1 2 2 3 3 3 5 5 5 5 ...
##  $ tax    : num  296 242 242 222 222 222 311 311 311 311 ...
##  $ ptratio: num  15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
##  $ b      : num  397 397 393 395 397 ...
##  $ lstat  : num  4.98 9.14 4.03 2.94 5.33 ...
##  $ medv   : num  24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
summary(BostonHousing)
##       crim                zn             indus       chas   
##  Min.   : 0.00632   Min.   :  0.00   Min.   : 0.46   0:471  
##  1st Qu.: 0.08204   1st Qu.:  0.00   1st Qu.: 5.19   1: 35  
##  Median : 0.25651   Median :  0.00   Median : 9.69          
##  Mean   : 3.61352   Mean   : 11.36   Mean   :11.14          
##  3rd Qu.: 3.67708   3rd Qu.: 12.50   3rd Qu.:18.10          
##  Max.   :88.97620   Max.   :100.00   Max.   :27.74          
##       nox               rm             age              dis        
##  Min.   :0.3850   Min.   :3.561   Min.   :  2.90   Min.   : 1.130  
##  1st Qu.:0.4490   1st Qu.:5.886   1st Qu.: 45.02   1st Qu.: 2.100  
##  Median :0.5380   Median :6.208   Median : 77.50   Median : 3.207  
##  Mean   :0.5547   Mean   :6.285   Mean   : 68.57   Mean   : 3.795  
##  3rd Qu.:0.6240   3rd Qu.:6.623   3rd Qu.: 94.08   3rd Qu.: 5.188  
##  Max.   :0.8710   Max.   :8.780   Max.   :100.00   Max.   :12.127  
##       rad              tax           ptratio            b         
##  Min.   : 1.000   Min.   :187.0   Min.   :12.60   Min.   :  0.32  
##  1st Qu.: 4.000   1st Qu.:279.0   1st Qu.:17.40   1st Qu.:375.38  
##  Median : 5.000   Median :330.0   Median :19.05   Median :391.44  
##  Mean   : 9.549   Mean   :408.2   Mean   :18.46   Mean   :356.67  
##  3rd Qu.:24.000   3rd Qu.:666.0   3rd Qu.:20.20   3rd Qu.:396.23  
##  Max.   :24.000   Max.   :711.0   Max.   :22.00   Max.   :396.90  
##      lstat            medv      
##  Min.   : 1.73   Min.   : 5.00  
##  1st Qu.: 6.95   1st Qu.:17.02  
##  Median :11.36   Median :21.20  
##  Mean   :12.65   Mean   :22.53  
##  3rd Qu.:16.95   3rd Qu.:25.00  
##  Max.   :37.97   Max.   :50.00
model <- lm(medv ~ ., data = BostonHousing)
summary(model)
## 
## Call:
## lm(formula = medv ~ ., data = BostonHousing)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -15.595  -2.730  -0.518   1.777  26.199 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.646e+01  5.103e+00   7.144 3.28e-12 ***
## crim        -1.080e-01  3.286e-02  -3.287 0.001087 ** 
## zn           4.642e-02  1.373e-02   3.382 0.000778 ***
## indus        2.056e-02  6.150e-02   0.334 0.738288    
## chas1        2.687e+00  8.616e-01   3.118 0.001925 ** 
## nox         -1.777e+01  3.820e+00  -4.651 4.25e-06 ***
## rm           3.810e+00  4.179e-01   9.116  < 2e-16 ***
## age          6.922e-04  1.321e-02   0.052 0.958229    
## dis         -1.476e+00  1.995e-01  -7.398 6.01e-13 ***
## rad          3.060e-01  6.635e-02   4.613 5.07e-06 ***
## tax         -1.233e-02  3.760e-03  -3.280 0.001112 ** 
## ptratio     -9.527e-01  1.308e-01  -7.283 1.31e-12 ***
## b            9.312e-03  2.686e-03   3.467 0.000573 ***
## lstat       -5.248e-01  5.072e-02 -10.347  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.745 on 492 degrees of freedom
## Multiple R-squared:  0.7406, Adjusted R-squared:  0.7338 
## F-statistic: 108.1 on 13 and 492 DF,  p-value: < 2.2e-16
plot(model)

plot(BostonHousing)

resid(model)
##             1             2             3             4             5 
##  -6.003843377  -3.425562379   4.132403281   4.792963511   8.256475767 
##             6             7             8             9            10 
##   3.443715538  -0.101808268   7.564011571   4.976363147  -0.020262107 
##            11            12            13            14            15 
##  -3.999496511  -2.686795681   0.793478472   0.847097189  -1.083482050 
##            16            17            18            19            20 
##   0.602516792   2.572490209   0.588598653   4.021988943  -0.206136033 
##            21            22            23            24            25 
##   1.076142473   1.928963305  -0.632881292   0.693714654  -0.078338315 
##            26            27            28            29            30 
##   0.513314391   1.136023454   0.091525719  -1.147372851   0.123571798 
##            31            32            33            34            35 
##   1.244882410  -3.559232946   4.388942638  -1.182758141  -0.206758913 
##            36            37            38            39            40 
##  -4.914635265  -2.341937076  -2.108911425   1.784973884  -0.557625688 
##            41            42            43            44            45 
##   0.684897746  -1.420564139   0.096133720   0.090207275  -1.741491757 
##            46            47            48            49            50 
##  -2.796698175  -0.423200323  -1.436550884   5.293446228   2.193922486 
##            51            52            53            54            55 
##  -1.581525353  -3.472222849  -2.655850802  -0.649018091   3.538152299 
##            56            57            58            59            60 
##   4.247350534  -0.156869782  -1.509198062   1.524620129  -1.484935551 
##            61            62            63            64            65 
##   0.827419603  -2.511102080  -1.787428565   2.445911311   9.626913558 
##            66            67            68            69            70 
##  -6.861483581  -6.130565115   0.886614358  -0.021537857   0.115163673 
##            71            72            73            74            75 
##  -1.001488594  -0.042657705  -1.757449572  -0.642957120  -1.404997164 
##            76            77            78            79            80 
##  -2.566930200  -2.945454031  -2.556998184  -0.061982662  -2.128173732 
##            81            82            83            84            85 
##  -0.405769682  -3.094860862  -1.235762968  -2.158734819  -0.884566738 
##            86            87            88            89            90 
##  -1.190491951   0.331465769  -3.692764153  -7.074618272  -2.131106227 
##            91            92            93            94            95 
##  -4.519019396  -5.412667341  -6.041227624  -4.081055463  -6.439773649 
##            96            97            98            99           100 
##  -0.224599485  -3.327449776   2.918404819   8.685454127   0.948971987 
##           101           102           103           104           105 
##   2.919779811   0.905865254  -1.190136835  -1.011671288  -1.334825911 
##           106           107           108           109           110 
##   0.960059918   2.312440082  -0.350490259  -2.848291146  -0.372036695 
##           111           112           113           114           115 
##   1.050341357  -3.725867438  -1.973236377  -2.015483150  -6.672088813 
##           116           117           118           119           120 
##  -2.130255909  -2.177246257  -4.490432606   0.064216357  -1.491808732 
##           121           122           123           124           125 
##   0.083679289  -2.171077767  -0.057385558   0.933380226  -1.760998192 
##           126           127           128           129           130 
##  -1.081784462   1.082933672   1.021233160  -0.938685924   0.244267149 
##           131           132           133           134           135 
##  -0.835273989   0.189865979   2.938084338   2.641923269   2.343547618 
##           136           137           138           139           140 
##   0.837222650   1.521581166  -2.261639540  -0.514838967   1.351185252 
##           141           142           143           144           145 
##   0.428580681  10.411144917  -1.194954780   3.451185197   3.071776377 
##           146           147           148           149           150 
##   1.764146573  -0.220820580   6.085009797   8.081558607   0.595486263 
##           151           152           153           154           155 
##   0.661418471   1.298988306  -4.822825579   2.113981064  -5.366002281 
##           156           157           158           159           160 
##  -4.503759227  -0.521258907   8.040173031  -4.730172680  -2.267527694 
##           161           162           163           164           165 
##  -5.708276658  13.225329854   9.442341555   8.152718323  -2.088673791 
##           166           167           168           169           170 
##  -0.378892378  12.796525454   0.712512527  -2.602739553  -4.353821137 
##           171           172           173           174           175 
##  -5.155146605  -5.190828118   0.123427810  -5.471943077  -3.921943396 
##           176           177           178           179           180 
##  -1.322090555  -2.416693069  -4.537409794  -1.535719677   4.277684320 
##           181           182           183           184           185 
##   5.075595356   8.434478893   4.012126787   1.507619640   3.681799919 
##           186           187           188           189           190 
##   4.833521901  14.115027738  -1.424767220  -2.611991466   0.384900507 
##           191           192           193           194           195 
##   6.238905148   0.210658593   3.480812857  -1.012607714  -2.458710043 
##           196           197           198           199           200 
##   9.154442786  -2.827700792  -2.369208100  -0.104691164   4.806548382 
##           201           202           203           204           205 
##   2.256060935  -5.187195014   5.228516080   6.468068762   6.810501563 
##           206           207           208           209           210 
##  -0.090347964   0.717152885   4.645527863   0.905710080   2.994122821 
##           211           212           213           214           215 
##  -0.692510964   2.239572462  -0.338929215   2.880574461  12.580832628 
##           216           217           218           219           220 
##   0.489508521  -3.303347748   0.344812873  -3.415254636  -6.686527678 
##           221           222           223           224           225 
##  -6.484197456  -2.074566560  -4.640519579   0.354180104   6.428975473 
##           226           227           228           229           230 
##  10.185381330   0.013942450  -0.799532503  11.243347580   0.265884883 
##           231           232           233           234           235 
##  -0.184492269  -1.588372916   3.651895196  11.136713685  -2.713835225 
##           236           237           238           239           240 
##  -1.267055713  -5.000107449  -1.219871558  -4.727170566  -5.129406781 
##           241           242           243           244           245 
##  -5.293759384  -3.642624783  -1.920078909  -3.702084138   1.271424399 
##           246           247           248           249           250 
##   5.101087387   4.283612248   0.638155722   3.211686902   2.120108504 
##           251           252           253           254           255 
##   0.193664530  -0.242158213   4.680359930  12.854366255  -2.072283165 
##           256           257           258           259           260 
##  -0.795808866   6.488907610   6.697609573  -0.483614206  -4.889885936 
##           261           262           263           264           265 
##  -1.012115081   5.933686692   7.810714991  -3.446340893   0.666024531 
##           266           267           268           269           270 
##  -5.445742999  -0.526735926   9.160442504   4.182076069  -5.008179052 
##           271           272           273           274           275 
##  -1.202955330  -2.003409719  -4.111694720  -0.276765982  -3.706391643 
##           276           277           278           279           280 
##  -1.796682735  -2.410858579  -1.739933819  -1.251926558  -0.209807009 
##           281           282           283           284           285 
##   6.602430340   1.068768140   5.660369252   5.326916613   0.603109145 
##           286           287           288           289           290 
##  -5.356592298  -0.001741541  -3.842066736  -4.913645804  -2.113958394 
##           291           292           293           294           295 
##  -4.935633106   2.896503670  -3.933398201  -1.917832369  -2.729823483 
##           296           297           298           299           300 
##   0.142356632  -0.262669985   0.760712419  -6.613098441  -2.910546108 
##           301           302           303           304           305 
##  -5.971594485  -6.942758713  -2.481910224   0.301127681   2.890945441 
##           306           307           308           309           310 
##  -2.368317923  -2.162268570  -4.509051237  -5.842442366  -3.289658271 
##           311           312           313           314           315 
##  -2.442668968  -4.778898429  -3.881339788  -3.945802461  -1.681200571 
##           316           317           318           319           320 
##  -4.339099011   0.184274272   1.424183136  -1.190702769  -0.325290392 
##           321           322           323           324           325 
##  -1.086822441  -1.769372815  -2.469524474  -0.951237906  -0.117834011 
##           326           327           328           329           330 
##  -0.067869132  -0.680761771   2.859103837  -1.874181054  -1.652490735 
##           331           332           333           334           335 
##  -1.792608939  -2.884466054  -3.938879998   0.059393075  -0.855099290 
##           336           337           338           339           340 
##   0.481270932  -0.660971763  -0.784903873  -1.566723204  -2.249657744 
##           341           342           343           344           345 
##  -2.729393054   2.372112040  -5.547349751  -3.806479125   2.652058832 
##           346           347           348           349           350 
##   0.954988786   2.416403590  -2.173800817  -3.042051174   4.451624376 
##           351           352           353           354           355 
##   2.440559053   3.553945773   1.719361734   4.697464942   3.875133681 
##           356           357           358           359           360 
##   4.005115383  -1.837046913  -1.018066075   0.497811132   3.394519426 
##           361           362           363           364           365 
##   2.333838949   0.968073818   2.571531957  -3.431508114 -15.594473900 
##           366           367           368           369           370 
##  13.218092659   6.357137517  12.268376759  26.199270978  17.355926399 
##           371           372           373           374           375 
##  15.393159576  25.056686675  24.000190899   7.673675004  13.022201940 
##           376           377           378           379           380 
## -10.307130642  -3.840610645  -6.932744140  -2.733313008  -6.635125872 
##           381           382           383           384           385 
##  -3.969948255  -7.576828331  -2.127682805  -0.761775118   5.520818839 
##           386           387           388           389           390 
##  -0.860221708   4.371578036   1.781351900   3.748014302  -2.707647352 
##           391           392           393           394           395 
##  -2.112251831   5.901127348  -0.191166432  -6.421241935  -5.241811753 
##           396           397           398           399           400 
##  -7.204457827  -6.795590755  -7.836327793  -1.551623191  -4.590167781 
##           401           402           403           404           405 
##  -6.281458707 -10.611745068  -6.161265872  -4.679487807   1.121836393 
##           406           407           408           409           410 
##  -3.211158614   3.833738070   7.917052140   3.492436308   7.647315455 
##           411           412           413           414           415 
##  -0.223082977   0.239280190  16.181481932   4.494216128  11.281310709 
##           416           417           418           419           420 
##  -2.383767369  -5.866608110   3.504376374   2.652201480  -6.206617942 
##           421           422           423           424           425 
##  -2.900026702  -3.924274759   2.278228678   0.224713895  -2.926176242 
##           426           427           428           429           430 
##  -1.623749760  -6.145906467  -3.175194255  -3.257562426  -3.542347875 
##           431           432           433           434           435 
##  -3.659556934  -4.595543541  -5.427283002  -2.731418609  -4.260904353 
##           436           437           438           439           440 
##   0.038583893  -4.920793839  -0.119760054   3.532488977  -0.265913130 
##           441           442           443           444           445 
##  -2.206096993  -0.195580592  -0.340485002  -2.659010295  -0.714746834 
##           446           447           448           449           450 
##  -0.174003587  -2.783446185  -5.526952390  -3.418346504  -4.227425073 
##           451           452           453           454           455 
##  -3.122716313  -4.212910951  -2.482152362  -4.689447908  -0.380001330 
##           456           457           458           459           460 
##  -1.720893352   0.012744187   0.623662098  -2.286685309   1.487523907 
##           461           462           463           464           465 
##  -2.648605332  -2.472089273  -0.274073197  -2.229407679   1.080881457 
##           466           467           468           469           470 
##   2.013837468   4.625214772   2.152231493   2.115942378   1.511616032 
##           471           472           473           474           475 
##  -0.267194411  -3.377180317   0.744192736   4.221753735  -2.591476316 
##           476           477           478           479           480 
##  -2.811462804  -3.834815996   0.457272618  -4.604963045  -0.462763906 
##           481           482           483           484           485 
##  -0.468788663  -3.398873155  -3.569943017   0.716012169   1.144838044 
##           486           487           488           489           490 
##  -1.022259141  -0.555919608  -0.725361044   3.344162834  -1.223866868 
##           491           492           493           494           495 
##   4.436003280  -0.159085382   4.168814549   1.173379456   3.887505860 
##           496           497           498           499           500 
##   6.214580361   5.686792129  -0.808541439  -0.098051744  -0.954988409 
##           501           502           503           504           505 
##  -3.668708470  -1.133340547  -1.775718920  -3.727426095  -4.127966807 
##           506 
## -10.444212293
fitted(model)
##          1          2          3          4          5          6 
## 30.0038434 25.0255624 30.5675967 28.6070365 27.9435242 25.2562845 
##          7          8          9         10         11         12 
## 23.0018083 19.5359884 11.5236369 18.9202621 18.9994965 21.5867957 
##         13         14         15         16         17         18 
## 20.9065215 19.5529028 19.2834821 19.2974832 20.5275098 16.9114013 
##         19         20         21         22         23         24 
## 16.1780111 18.4061360 12.5238575 17.6710367 15.8328813 13.8062853 
##         25         26         27         28         29         30 
## 15.6783383 13.3866856 15.4639765 14.7084743 19.5473729 20.8764282 
##         31         32         33         34         35         36 
## 11.4551176 18.0592329  8.8110574 14.2827581 13.7067589 23.8146353 
##         37         38         39         40         41         42 
## 22.3419371 23.1089114 22.9150261 31.3576257 34.2151023 28.0205641 
##         43         44         45         46         47         48 
## 25.2038663 24.6097927 22.9414918 22.0966982 20.4232003 18.0365509 
##         49         50         51         52         53         54 
##  9.1065538 17.2060775 21.2815254 23.9722228 27.6558508 24.0490181 
##         55         56         57         58         59         60 
## 15.3618477 31.1526495 24.8568698 33.1091981 21.7753799 21.0849356 
##         61         62         63         64         65         66 
## 17.8725804 18.5111021 23.9874286 22.5540887 23.3730864 30.3614836 
##         67         68         69         70         71         72 
## 25.5305651 21.1133856 17.4215379 20.7848363 25.2014886 21.7426577 
##         73         74         75         76         77         78 
## 24.5574496 24.0429571 25.5049972 23.9669302 22.9454540 23.3569982 
##         79         80         81         82         83         84 
## 21.2619827 22.4281737 28.4057697 26.9948609 26.0357630 25.0587348 
##         85         86         87         88         89         90 
## 24.7845667 27.7904920 22.1685342 25.8927642 30.6746183 30.8311062 
##         91         92         93         94         95         96 
## 27.1190194 27.4126673 28.9412276 29.0810555 27.0397736 28.6245995 
##         97         98         99        100        101        102 
## 24.7274498 35.7815952 35.1145459 32.2510280 24.5802202 25.5941347 
##        103        104        105        106        107        108 
## 19.7901368 20.3116713 21.4348259 18.5399401 17.1875599 20.7504903 
##        109        110        111        112        113        114 
## 22.6482911 19.7720367 20.6496586 26.5258674 20.7732364 20.7154831 
##        115        116        117        118        119        120 
## 25.1720888 20.4302559 23.3772463 23.6904326 20.3357836 20.7918087 
##        121        122        123        124        125        126 
## 21.9163207 22.4710778 20.5573856 16.3666198 20.5609982 22.4817845 
##        127        128        129        130        131        132 
## 14.6170663 15.1787668 18.9386859 14.0557329 20.0352740 19.4101340 
##        133        134        135        136        137        138 
## 20.0619157 15.7580767 13.2564524 17.2627773 15.8784188 19.3616395 
##        139        140        141        142        143        144 
## 13.8148390 16.4488147 13.5714193  3.9888551 14.5949548 12.1488148 
##        145        146        147        148        149        150 
##  8.7282236 12.0358534 15.8208206  8.5149902  9.7184414 14.8045137 
##        151        152        153        154        155        156 
## 20.8385815 18.3010117 20.1228256 17.2860189 22.3660023 20.1037592 
##        157        158        159        160        161        162 
## 13.6212589 33.2598270 29.0301727 25.5675277 32.7082767 36.7746701 
##        163        164        165        166        167        168 
## 40.5576584 41.8472817 24.7886738 25.3788924 37.2034745 23.0874875 
##        169        170        171        172        173        174 
## 26.4027396 26.6538211 22.5551466 24.2908281 22.9765722 29.0719431 
##        175        176        177        178        179        180 
## 26.5219434 30.7220906 25.6166931 29.1374098 31.4357197 32.9223157 
##        181        182        183        184        185        186 
## 34.7244046 27.7655211 33.8878732 30.9923804 22.7182001 24.7664781 
##        187        188        189        190        191        192 
## 35.8849723 33.4247672 32.4119915 34.5150995 30.7610949 30.2893414 
##        193        194        195        196        197        198 
## 32.9191871 32.1126077 31.5587100 40.8455572 36.1277008 32.6692081 
##        199        200        201        202        203        204 
## 34.7046912 30.0934516 30.6439391 29.2871950 37.0714839 42.0319312 
##        205        206        207        208        209        210 
## 43.1894984 22.6903480 23.6828471 17.8544721 23.4942899 17.0058772 
##        211        212        213        214        215        216 
## 22.3925110 17.0604275 22.7389292 25.2194255 11.1191674 24.5104915 
##        217        218        219        220        221        222 
## 26.6033477 28.3551871 24.9152546 29.6865277 33.1841975 23.7745666 
##        223        224        225        226        227        228 
## 32.1405196 29.7458199 38.3710245 39.8146187 37.5860575 32.3995325 
##        229        230        231        232        233        234 
## 35.4566524 31.2341151 24.4844923 33.2883729 38.0481048 37.1632863 
##        235        236        237        238        239        240 
## 31.7138352 25.2670557 30.1001074 32.7198716 28.4271706 28.4294068 
##        241        242        243        244        245        246 
## 27.2937594 23.7426248 24.1200789 27.4020841 16.3285756 13.3989126 
##        247        248        249        250        251        252 
## 20.0163878 19.8618443 21.2883131 24.0798915 24.2063355 25.0421582 
##        253        254        255        256        257        258 
## 24.9196401 29.9456337 23.9722832 21.6958089 37.5110924 43.3023904 
##        259        260        261        262        263        264 
## 36.4836142 34.9898859 34.8121151 37.1663133 40.9892850 34.4463409 
##        265        266        267        268        269        270 
## 35.8339755 28.2457430 31.2267359 40.8395575 39.3179239 25.7081791 
##        271        272        273        274        275        276 
## 22.3029553 27.2034097 28.5116947 35.4767660 36.1063916 33.7966827 
##        277        278        279        280        281        282 
## 35.6108586 34.8399338 30.3519266 35.3098070 38.7975697 34.3312319 
##        283        284        285        286        287        288 
## 40.3396307 44.6730834 31.5968909 27.3565923 20.1017415 27.0420667 
##        289        290        291        292        293        294 
## 27.2136458 26.9139584 33.4356331 34.4034963 31.8333982 25.8178324 
##        295        296        297        298        299        300 
## 24.4298235 28.4576434 27.3626700 19.5392876 29.1130984 31.9105461 
##        301        302        303        304        305        306 
## 30.7715945 28.9427587 28.8819102 32.7988723 33.2090546 30.7683179 
##        307        308        309        310        311        312 
## 35.5622686 32.7090512 28.6424424 23.5896583 18.5426690 26.8788984 
##        313        314        315        316        317        318 
## 23.2813398 25.5458025 25.4812006 20.5390990 17.6157257 18.3758169 
##        319        320        321        322        323        324 
## 24.2907028 21.3252904 24.8868224 24.8693728 22.8695245 19.4512379 
##        325        326        327        328        329        330 
## 25.1178340 24.6678691 23.6807618 19.3408962 21.1741811 24.2524907 
##        331        332        333        334        335        336 
## 21.5926089 19.9844661 23.3388800 22.1406069 21.5550993 20.6187291 
##        337        338        339        340        341        342 
## 20.1609718 19.2849039 22.1667232 21.2496577 21.4293931 30.3278880 
##        343        344        345        346        347        348 
## 22.0473498 27.7064791 28.5479412 16.5450112 14.7835964 25.2738008 
##        349        350        351        352        353        354 
## 27.5420512 22.1483756 20.4594409 20.5460542 16.8806383 25.4025351 
##        355        356        357        358        359        360 
## 14.3248663 16.5948846 19.6370469 22.7180661 22.2021889 19.2054806 
##        361        362        363        364        365        366 
## 22.6661611 18.9319262 18.2284680 20.2315081 37.4944739 14.2819073 
##        367        368        369        370        371        372 
## 15.5428625 10.8316232 23.8007290 32.6440736 34.6068404 24.9433133 
##        373        374        375        376        377        378 
## 25.9998091  6.1263250  0.7777981 25.3071306 17.7406106 20.2327441 
##        379        380        381        382        383        384 
## 15.8333130 16.8351259 14.3699483 18.4768283 13.4276828 13.0617751 
##        385        386        387        388        389        390 
##  3.2791812  8.0602217  6.1284220  5.6186481  6.4519857 14.2076474 
##        391        392        393        394        395        396 
## 17.2122518 17.2988727  9.8911664 20.2212419 17.9418118 20.3044578 
##        397        398        399        400        401        402 
## 19.2955908 16.3363278  6.5516232 10.8901678 11.8814587 17.8117451 
##        403        404        405        406        407        408 
## 18.2612659 12.9794878  7.3781636  8.2111586  8.0662619 19.9829479 
##        409        410        411        412        413        414 
## 13.7075637 19.8526845 15.2230830 16.9607198  1.7185181 11.8057839 
##        415        416        417        418        419        420 
## -4.2813107  9.5837674 13.3666081  6.8956236  6.1477985 14.6066179 
##        421        422        423        424        425        426 
## 19.6000267 18.1242748 18.5217713 13.1752861 14.6261762  9.9237498 
##        427        428        429        430        431        432 
## 16.3459065 14.0751943 14.2575624 13.0423479 18.1595569 18.6955435 
##        433        434        435        436        437        438 
## 21.5272830 17.0314186 15.9609044 13.3614161 14.5207938  8.8197601 
##        439        440        441        442        443        444 
##  4.8675110 13.0659131 12.7060970 17.2955806 18.7404850 18.0590103 
##        445        446        447        448        449        450 
## 11.5147468 11.9740036 17.6834462 18.1269524 17.5183465 17.2274251 
##        451        452        453        454        455        456 
## 16.5227163 19.4129110 18.5821524 22.4894479 15.2800013 15.8208934 
##        457        458        459        460        461        462 
## 12.6872558 12.8763379 17.1866853 18.5124761 19.0486053 20.1720893 
##        463        464        465        466        467        468 
## 19.7740732 22.4294077 20.3191185 17.8861625 14.3747852 16.9477685 
##        469        470        471        472        473        474 
## 16.9840576 18.5883840 20.1671944 22.9771803 22.4558073 25.5782463 
##        475        476        477        478        479        480 
## 16.3914763 16.1114628 20.5348160 11.5427274 19.2049630 21.8627639 
##        481        482        483        484        485        486 
## 23.4687887 27.0988732 28.5699430 21.0839878 19.4551620 22.2222591 
##        487        488        489        490        491        492 
## 19.6559196 21.3253610 11.8558372  8.2238669  3.6639967 13.7590854 
##        493        494        495        496        497        498 
## 15.9311855 20.6266205 20.6124941 16.8854196 14.0132079 19.1085414 
##        499        500        501        502        503        504 
## 21.2980517 18.4549884 20.4687085 23.5333405 22.3757189 27.6274261 
##        505        506 
## 26.1279668 22.3442123
par(mfrow=c(2,2))
plot(model)

model2 <- lm(medv ~ crim + zn +indus + nox + rm + age + dis + rad, data = BostonHousing)
summary(model2)
## 
## Call:
## lm(formula = medv ~ crim + zn + indus + nox + rm + age + dis + 
##     rad, data = BostonHousing)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -15.808  -3.065  -0.666   2.062  38.058 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -1.31945    4.09090  -0.323 0.747184    
## crim         -0.18960    0.03877  -4.891 1.36e-06 ***
## zn            0.06209    0.01541   4.030 6.45e-05 ***
## indus        -0.20164    0.06535  -3.085 0.002145 ** 
## nox         -12.32881    4.31920  -2.854 0.004492 ** 
## rm            6.99462    0.41206  16.975  < 2e-16 ***
## age          -0.05495    0.01493  -3.681 0.000258 ***
## dis          -1.78329    0.23963  -7.442 4.40e-13 ***
## rad          -0.05302    0.04445  -1.193 0.233511    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.739 on 497 degrees of freedom
## Multiple R-squared:  0.6168, Adjusted R-squared:  0.6107 
## F-statistic:   100 on 8 and 497 DF,  p-value: < 2.2e-16
plot(model2)

library('chemometrics')
## Loading required package: rpart
data("mtcars")
summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
m <- lm(cbind(mtcars$mpg,mtcars$hp) ~
          mtcars$cyl + mtcars$disp + mtcars$wt)
summary(m)
## Response mtcars$mpg :
## 
## Call:
## lm(formula = `mtcars$mpg` ~ mtcars$cyl + mtcars$disp + mtcars$wt)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.4035 -1.4028 -0.4955  1.3387  6.0722 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 41.107678   2.842426  14.462 1.62e-14 ***
## mtcars$cyl  -1.784944   0.607110  -2.940  0.00651 ** 
## mtcars$disp  0.007473   0.011845   0.631  0.53322    
## mtcars$wt   -3.635677   1.040138  -3.495  0.00160 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.595 on 28 degrees of freedom
## Multiple R-squared:  0.8326, Adjusted R-squared:  0.8147 
## F-statistic: 46.42 on 3 and 28 DF,  p-value: 5.399e-11
## 
## 
## Response mtcars$hp :
## 
## Call:
## lm(formula = `mtcars$hp` ~ mtcars$cyl + mtcars$disp + mtcars$wt)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -54.39 -22.30  -9.33  18.64 134.56 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -13.5912    42.8244  -0.317   0.7533  
## mtcars$cyl   23.9368     9.1468   2.617   0.0141 *
## mtcars$disp   0.2009     0.1785   1.126   0.2698  
## mtcars$wt   -10.6253    15.6708  -0.678   0.5033  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 39.09 on 28 degrees of freedom
## Multiple R-squared:  0.7064, Adjusted R-squared:  0.6749 
## F-statistic: 22.46 on 3 and 28 DF,  p-value: 1.306e-07
mm <- manova(m)
mm
## Call:
##    manova(m)
## 
## Terms:
##                 mtcars$cyl mtcars$disp mtcars$wt Residuals
## resp 1              817.71       37.59     82.25    188.49
## resp 2           100984.17     1254.66    702.48  42785.56
## Deg. of Freedom          1           1         1        28
## 
## Residual standard errors: 2.594585 39.09035
## Estimated effects may be unbalanced
summary(mm)
##             Df  Pillai approx F num Df den Df    Pr(>F)    
## mtcars$cyl   1 0.83917   70.439      2     27 1.932e-11 ***
## mtcars$disp  1 0.16712    2.709      2     27    0.0847 .  
## mtcars$wt    1 0.35841    7.542      2     27    0.0025 ** 
## Residuals   28                                             
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
cor(mtcars)
##             mpg        cyl       disp         hp        drat         wt
## mpg   1.0000000 -0.8521620 -0.8475514 -0.7761684  0.68117191 -0.8676594
## cyl  -0.8521620  1.0000000  0.9020329  0.8324475 -0.69993811  0.7824958
## disp -0.8475514  0.9020329  1.0000000  0.7909486 -0.71021393  0.8879799
## hp   -0.7761684  0.8324475  0.7909486  1.0000000 -0.44875912  0.6587479
## drat  0.6811719 -0.6999381 -0.7102139 -0.4487591  1.00000000 -0.7124406
## wt   -0.8676594  0.7824958  0.8879799  0.6587479 -0.71244065  1.0000000
## qsec  0.4186840 -0.5912421 -0.4336979 -0.7082234  0.09120476 -0.1747159
## vs    0.6640389 -0.8108118 -0.7104159 -0.7230967  0.44027846 -0.5549157
## am    0.5998324 -0.5226070 -0.5912270 -0.2432043  0.71271113 -0.6924953
## gear  0.4802848 -0.4926866 -0.5555692 -0.1257043  0.69961013 -0.5832870
## carb -0.5509251  0.5269883  0.3949769  0.7498125 -0.09078980  0.4276059
##             qsec         vs          am       gear        carb
## mpg   0.41868403  0.6640389  0.59983243  0.4802848 -0.55092507
## cyl  -0.59124207 -0.8108118 -0.52260705 -0.4926866  0.52698829
## disp -0.43369788 -0.7104159 -0.59122704 -0.5555692  0.39497686
## hp   -0.70822339 -0.7230967 -0.24320426 -0.1257043  0.74981247
## drat  0.09120476  0.4402785  0.71271113  0.6996101 -0.09078980
## wt   -0.17471588 -0.5549157 -0.69249526 -0.5832870  0.42760594
## qsec  1.00000000  0.7445354 -0.22986086 -0.2126822 -0.65624923
## vs    0.74453544  1.0000000  0.16834512  0.2060233 -0.56960714
## am   -0.22986086  0.1683451  1.00000000  0.7940588  0.05753435
## gear -0.21268223  0.2060233  0.79405876  1.0000000  0.27407284
## carb -0.65624923 -0.5696071  0.05753435  0.2740728  1.00000000
cov(mtcars)
##              mpg         cyl        disp          hp         drat
## mpg    36.324103  -9.1723790  -633.09721 -320.732056   2.19506351
## cyl    -9.172379   3.1895161   199.66028  101.931452  -0.66836694
## disp -633.097208 199.6602823 15360.79983 6721.158669 -47.06401915
## hp   -320.732056 101.9314516  6721.15867 4700.866935 -16.45110887
## drat    2.195064  -0.6683669   -47.06402  -16.451109   0.28588135
## wt     -5.116685   1.3673710   107.68420   44.192661  -0.37272073
## qsec    4.509149  -1.8868548   -96.05168  -86.770081   0.08714073
## vs      2.017137  -0.7298387   -44.37762  -24.987903   0.11864919
## am      1.803931  -0.4657258   -36.56401   -8.320565   0.19015121
## gear    2.135685  -0.6491935   -50.80262   -6.358871   0.27598790
## carb   -5.363105   1.5201613    79.06875   83.036290  -0.07840726
##               wt         qsec           vs           am        gear
## mpg   -5.1166847   4.50914919   2.01713710   1.80393145   2.1356855
## cyl    1.3673710  -1.88685484  -0.72983871  -0.46572581  -0.6491935
## disp 107.6842040 -96.05168145 -44.37762097 -36.56401210 -50.8026210
## hp    44.1926613 -86.77008065 -24.98790323  -8.32056452  -6.3588710
## drat  -0.3727207   0.08714073   0.11864919   0.19015121   0.2759879
## wt     0.9573790  -0.30548161  -0.27366129  -0.33810484  -0.4210806
## qsec  -0.3054816   3.19316613   0.67056452  -0.20495968  -0.2804032
## vs    -0.2736613   0.67056452   0.25403226   0.04233871   0.0766129
## am    -0.3381048  -0.20495968   0.04233871   0.24899194   0.2923387
## gear  -0.4210806  -0.28040323   0.07661290   0.29233871   0.5443548
## carb   0.6757903  -1.89411290  -0.46370968   0.04637097   0.3266129
##             carb
## mpg  -5.36310484
## cyl   1.52016129
## disp 79.06875000
## hp   83.03629032
## drat -0.07840726
## wt    0.67579032
## qsec -1.89411290
## vs   -0.46370968
## am    0.04637097
## gear  0.32661290
## carb  2.60887097

Robust Regression

mtcars2 <- subset(mtcars, select = c(1:3,5:11))
prcomp(mtcars2)
## Standard deviations (1, .., p=10):
##  [1] 124.0636883   3.3156055   1.9727388   0.9707287   0.6644391
##  [6]   0.3146818   0.2859604   0.2664605   0.2147193   0.2018027
## 
## Rotation (n x k) = (10 x 10):
##               PC1         PC2          PC3         PC4          PC5
## mpg  -0.041203129  0.95183084  0.232722413  0.12056369  0.143485224
## cyl   0.012988407 -0.10484001  0.159076414 -0.27289350  0.798327666
## disp  0.998991829  0.04314172  0.002130588  0.00748304 -0.007924665
## drat -0.003061490  0.01699931  0.088999116  0.09937560 -0.233250086
## wt    0.007005058 -0.05829569 -0.112386522  0.24558785  0.115318084
## qsec -0.006249938  0.11072226 -0.751430367  0.49520763  0.201798940
## vs   -0.002886954  0.02708815 -0.112674747  0.09517980 -0.093584346
## am   -0.002378581  0.01613746  0.154271439  0.01337939 -0.209439675
## gear -0.003303766 -0.01612221  0.229043235  0.22570301 -0.342547528
## carb  0.005149505 -0.25291208  0.496361949  0.73166795  0.252257083
##                PC6           PC7           PC8          PC9         PC10
## mpg  -0.0400228143  0.0226924552 -0.0003540778  0.030095664 -0.003790957
## cyl   0.4190888426 -0.1887906595  0.0462382810  0.111047902  0.161213070
## disp  0.0007329425 -0.0005858438 -0.0031957826 -0.004212149 -0.001395423
## drat  0.0103913419 -0.9384812124  0.0682470382  0.201427862 -0.037601701
## wt   -0.3056261176  0.1565490515  0.4696441895  0.704420274  0.274993899
## qsec  0.2684368135 -0.1027094089  0.0808239868 -0.215306288 -0.040045667
## vs    0.0672285498 -0.0027898119 -0.7236644285  0.236611153  0.620114966
## am    0.1456280922 -0.0223283455  0.4653387762 -0.458049655  0.695582133
## gear  0.7765594813  0.2179864535  0.0755825359  0.312507017 -0.158277041
## carb -0.1685725522  0.0038144361 -0.1419603525 -0.200379664 -0.043887856
d1<- cooks.distance(m)
d1
##            [,1]         [,2]
## 1  3.510367e-03 1.822065e+00
## 2  2.936643e-07 6.897965e-03
## 3  4.573918e-02 6.946566e-01
## 4  6.069887e-06 1.483451e-02
## 5  2.019247e-02 1.059792e+01
## 6  1.555998e-05 8.893810e-03
## 7  2.741556e-02 5.848365e+00
## 8  3.007257e-05 8.541479e-03
## 9  4.381135e-03 2.415355e+00
## 10 9.802341e-08 5.337843e-04
## 11 1.269762e-02 1.211628e-01
## 12 2.253562e-04 4.288834e-03
## 13 2.438907e-02 1.174039e+00
## 14 8.343661e-08 5.004033e-03
## 15 1.330855e-02 2.521798e+00
## 16 1.670128e-06 4.196937e-04
## 17 2.291895e-01 5.964357e+00
## 18 5.467712e-04 1.182479e-03
## 19 1.880896e-02 4.953478e+00
## 20 7.185953e-04 2.779920e-03
## 21 7.567035e-02 1.115400e+00
## 22 1.349353e-05 4.853181e-02
## 23 8.324926e-03 1.151130e+01
## 24 6.436269e-05 1.908001e-02
## 25 9.003823e-02 1.432997e+01
## 26 8.935852e-07 2.358305e-03
## 27 5.855017e-03 2.725187e-01
## 28 6.365207e-05 3.296419e-02
## 29 4.807696e-02 2.581010e+01
## 30 5.910588e-05 4.138886e-02
## 31 4.662546e-03 7.000564e+01
## 32 2.330744e-04 2.117389e-02
library(MASS)
m <- rlm(mpg ~ cyl + disp + wt + gear, data = mtcars)
m
## Call:
## rlm(formula = mpg ~ cyl + disp + wt + gear, data = mtcars)
## Converged in 8 iterations
## 
## Coefficients:
##  (Intercept)          cyl         disp           wt         gear 
## 43.193813720 -1.746917038  0.007003493 -3.767535205 -0.535293330 
## 
## Degrees of freedom: 32 total; 27 residual
## Scale estimate: 2.11
m2<- ltsreg(mpg ~ cyl + disp + wt + qsec,
data = mtcars)
m2
## Call:
## lqs.formula(formula = mpg ~ cyl + disp + wt + qsec, data = mtcars, 
##     method = "lts")
## 
## Coefficients:
## (Intercept)          cyl         disp           wt         qsec  
##    22.23042     -0.11731     -0.01894     -1.48761      0.33662  
## 
## Scale estimates 1.440 1.565
ltsreg(mpg ~ cyl + disp + wt + qsec,
data = mtcars)
## Call:
## lqs.formula(formula = mpg ~ cyl + disp + wt + qsec, data = mtcars, 
##     method = "lts")
## 
## Coefficients:
## (Intercept)          cyl         disp           wt         qsec  
##    28.91579     -0.41783     -0.01722     -1.44949      0.02073  
## 
## Scale estimates 1.3830 0.9423
ltsreg(mpg ~ cyl + disp + wt + qsec,
data = mtcars)
## Call:
## lqs.formula(formula = mpg ~ cyl + disp + wt + qsec, data = mtcars, 
##     method = "lts")
## 
## Coefficients:
## (Intercept)          cyl         disp           wt         qsec  
##    27.23779     -0.26495     -0.01926     -1.27250      0.06052  
## 
## Scale estimates 1.4092 0.9238