# From the book ISLR by Games,Witten,Hastie and Tibshirani
library(ISLR)
objects(grep("ISLR",search()))
##  [1] "Auto"      "Caravan"   "Carseats"  "College"   "Default"  
##  [6] "Hitters"   "Khan"      "NCI60"     "OJ"        "Portfolio"
## [11] "Smarket"   "Wage"      "Weekly"
data("Carseats")
Carseats[1:5,]
##   Sales CompPrice Income Advertising Population Price ShelveLoc Age
## 1  9.50       138     73          11        276   120       Bad  42
## 2 11.22       111     48          16        260    83      Good  65
## 3 10.06       113     35          10        269    80    Medium  59
## 4  7.40       117    100           4        466    97    Medium  55
## 5  4.15       141     64           3        340   128       Bad  38
##   Education Urban  US
## 1        17   Yes Yes
## 2        10   Yes Yes
## 3        12   Yes Yes
## 4        14   Yes Yes
## 5        13   Yes  No
#?Carseats
str(Carseats)
## 'data.frame':    400 obs. of  11 variables:
##  $ Sales      : num  9.5 11.22 10.06 7.4 4.15 ...
##  $ CompPrice  : num  138 111 113 117 141 124 115 136 132 132 ...
##  $ Income     : num  73 48 35 100 64 113 105 81 110 113 ...
##  $ Advertising: num  11 16 10 4 3 13 0 15 0 0 ...
##  $ Population : num  276 260 269 466 340 501 45 425 108 131 ...
##  $ Price      : num  120 83 80 97 128 72 108 120 124 124 ...
##  $ ShelveLoc  : Factor w/ 3 levels "Bad","Good","Medium": 1 2 3 3 1 1 3 2 3 3 ...
##  $ Age        : num  42 65 59 55 38 78 71 67 76 76 ...
##  $ Education  : num  17 10 12 14 13 16 15 10 10 17 ...
##  $ Urban      : Factor w/ 2 levels "No","Yes": 2 2 2 2 2 1 2 2 1 1 ...
##  $ US         : Factor w/ 2 levels "No","Yes": 2 2 2 2 1 2 1 2 1 2 ...
table(Carseats$ShelveLoc)
## 
##    Bad   Good Medium 
##     96     85    219
table(Carseats$Urban)
## 
##  No Yes 
## 118 282
table(Carseats$US)
## 
##  No Yes 
## 142 258
summary(Carseats)
##      Sales          CompPrice       Income        Advertising    
##  Min.   : 0.000   Min.   : 77   Min.   : 21.00   Min.   : 0.000  
##  1st Qu.: 5.390   1st Qu.:115   1st Qu.: 42.75   1st Qu.: 0.000  
##  Median : 7.490   Median :125   Median : 69.00   Median : 5.000  
##  Mean   : 7.496   Mean   :125   Mean   : 68.66   Mean   : 6.635  
##  3rd Qu.: 9.320   3rd Qu.:135   3rd Qu.: 91.00   3rd Qu.:12.000  
##  Max.   :16.270   Max.   :175   Max.   :120.00   Max.   :29.000  
##    Population        Price        ShelveLoc        Age       
##  Min.   : 10.0   Min.   : 24.0   Bad   : 96   Min.   :25.00  
##  1st Qu.:139.0   1st Qu.:100.0   Good  : 85   1st Qu.:39.75  
##  Median :272.0   Median :117.0   Medium:219   Median :54.50  
##  Mean   :264.8   Mean   :115.8                Mean   :53.32  
##  3rd Qu.:398.5   3rd Qu.:131.0                3rd Qu.:66.00  
##  Max.   :509.0   Max.   :191.0                Max.   :80.00  
##    Education    Urban       US     
##  Min.   :10.0   No :118   No :142  
##  1st Qu.:12.0   Yes:282   Yes:258  
##  Median :14.0                      
##  Mean   :13.9                      
##  3rd Qu.:16.0                      
##  Max.   :18.0
Carseats$ShelveLoc <- as.factor(Carseats$ShelveLoc)
Carseats$ShelveLoc <- factor(Carseats$ShelveLoc,levels = c("Bad","Medium","Good"))
class(Carseats$ShelveLoc)
## [1] "factor"
summary(Carseats)
##      Sales          CompPrice       Income        Advertising    
##  Min.   : 0.000   Min.   : 77   Min.   : 21.00   Min.   : 0.000  
##  1st Qu.: 5.390   1st Qu.:115   1st Qu.: 42.75   1st Qu.: 0.000  
##  Median : 7.490   Median :125   Median : 69.00   Median : 5.000  
##  Mean   : 7.496   Mean   :125   Mean   : 68.66   Mean   : 6.635  
##  3rd Qu.: 9.320   3rd Qu.:135   3rd Qu.: 91.00   3rd Qu.:12.000  
##  Max.   :16.270   Max.   :175   Max.   :120.00   Max.   :29.000  
##    Population        Price        ShelveLoc        Age       
##  Min.   : 10.0   Min.   : 24.0   Bad   : 96   Min.   :25.00  
##  1st Qu.:139.0   1st Qu.:100.0   Medium:219   1st Qu.:39.75  
##  Median :272.0   Median :117.0   Good  : 85   Median :54.50  
##  Mean   :264.8   Mean   :115.8                Mean   :53.32  
##  3rd Qu.:398.5   3rd Qu.:131.0                3rd Qu.:66.00  
##  Max.   :509.0   Max.   :191.0                Max.   :80.00  
##    Education    Urban       US     
##  Min.   :10.0   No :118   No :142  
##  1st Qu.:12.0   Yes:282   Yes:258  
##  Median :14.0                      
##  Mean   :13.9                      
##  3rd Qu.:16.0                      
##  Max.   :18.0
aggregate(Carseats$Sales~Carseats$ShelveLoc+Carseats$Urban,Carseats,mean)
##   Carseats$ShelveLoc Carseats$Urban Carseats$Sales
## 1                Bad             No       5.547273
## 2             Medium             No       7.240882
## 3               Good             No       9.931429
## 4                Bad            Yes       5.515676
## 5             Medium            Yes       7.336159
## 6               Good            Yes      10.352807
aggregate(Carseats$Sales~Carseats$ShelveLoc+Carseats$Advertising,Carseats,mean)
##    Carseats$ShelveLoc Carseats$Advertising Carseats$Sales
## 1                 Bad                    0       5.109429
## 2              Medium                    0       6.720000
## 3                Good                    0       9.270714
## 4                 Bad                    1       3.150000
## 5              Medium                    1       5.752000
## 6                Good                    1      11.395000
## 7                 Bad                    2       5.150000
## 8              Medium                    2       6.295000
## 9                Good                    2       9.245000
## 10                Bad                    3       5.475000
## 11             Medium                    3       7.613333
## 12               Good                    3      12.660000
## 13                Bad                    4       4.514000
## 14             Medium                    4       6.555000
## 15               Good                    4      11.706667
## 16                Bad                    5       6.590000
## 17             Medium                    5       6.194167
## 18               Good                    5       9.750000
## 19                Bad                    6       4.635000
## 20             Medium                    6       5.964000
## 21                Bad                    7       3.593333
## 22             Medium                    7       7.791667
## 23               Good                    7      11.235000
## 24                Bad                    8       7.690000
## 25             Medium                    8       8.108571
## 26               Good                    8       8.440000
## 27                Bad                    9       8.320000
## 28             Medium                    9       6.508000
## 29               Good                    9      10.440000
## 30                Bad                   10       5.853333
## 31             Medium                   10       8.413636
## 32               Good                   10      10.257500
## 33                Bad                   11       6.128333
## 34             Medium                   11       7.441667
## 35               Good                   11       9.972500
## 36                Bad                   12       6.150000
## 37             Medium                   12       8.464545
## 38               Good                   12       9.840000
## 39                Bad                   13       6.711667
## 40             Medium                   13       7.268889
## 41               Good                   13       8.994000
## 42             Medium                   14       8.591000
## 43               Good                   14      13.440000
## 44                Bad                   15       5.127500
## 45             Medium                   15       8.310000
## 46               Good                   15      11.410000
## 47                Bad                   16       9.075000
## 48             Medium                   16       8.875000
## 49               Good                   16      11.180000
## 50             Medium                   17       7.745000
## 51               Good                   17      11.670000
## 52                Bad                   18       1.420000
## 53             Medium                   18       9.534000
## 54                Bad                   19       6.835000
## 55             Medium                   19       8.300000
## 56               Good                   19      13.445000
## 57                Bad                   20       6.900000
## 58             Medium                   20       5.740000
## 59               Good                   20      12.980000
## 60                Bad                   21       3.900000
## 61                Bad                   22       7.680000
## 62             Medium                   22      10.260000
## 63                Bad                   23       8.550000
## 64               Good                   23       9.580000
## 65               Good                   24      12.490000
## 66             Medium                   25       8.750000
## 67             Medium                   26       8.030000
## 68             Medium                   29       9.530000
x <- seq(1,10)
y <- x
plot(x,y,col="green")
f <-outer(x,y,function(x,y)cos(y)/(1+x^2))
contour(x,y,f,nlevels = 45,add = T)

fa <- (f-t(f))/2
contour(x,y,fa,nlevels = 15)

image(x,y,fa)

persp(x,y,fa)

persp(x,y,fa,theta = 30)

persp(x,y,fa,theta = 30,phi = 20)

persp(x,y,fa,theta = 30,phi = 70)

persp(x,y,fa,theta = 30,phi = 40)

A <- matrix(1:16,4,4)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16
A[c(1,3)]
## [1] 1 3
A[,c(2,4)]
##      [,1] [,2]
## [1,]    5   13
## [2,]    6   14
## [3,]    7   15
## [4,]    8   16
A[c(1,3),c(2,4)]
##      [,1] [,2]
## [1,]    5   13
## [2,]    7   15
data("Auto")
#fix(Auto)
setwd("C:\\Users\\Luis\\Desktop\\ISLR")
auto <- read.csv("C:\\Users\\Luis\\Desktop\\ISLR\\Auto.csv",
                   header = TRUE,
                   sep = ",")
auto[1:5,]
##   mpg cylinders displacement horsepower weight acceleration year origin
## 1  18         8          307        130   3504         12.0   70      1
## 2  15         8          350        165   3693         11.5   70      1
## 3  18         8          318        150   3436         11.0   70      1
## 4  16         8          304        150   3433         12.0   70      1
## 5  17         8          302        140   3449         10.5   70      1
##                        name
## 1 chevrolet chevelle malibu
## 2         buick skylark 320
## 3        plymouth satellite
## 4             amc rebel sst
## 5               ford torino
str(auto)
## 'data.frame':    397 obs. of  9 variables:
##  $ mpg         : num  18 15 18 16 17 15 14 14 14 15 ...
##  $ cylinders   : int  8 8 8 8 8 8 8 8 8 8 ...
##  $ displacement: num  307 350 318 304 302 429 454 440 455 390 ...
##  $ horsepower  : Factor w/ 94 levels "?","100","102",..: 17 35 29 29 24 42 47 46 48 40 ...
##  $ weight      : int  3504 3693 3436 3433 3449 4341 4354 4312 4425 3850 ...
##  $ acceleration: num  12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
##  $ year        : int  70 70 70 70 70 70 70 70 70 70 ...
##  $ origin      : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ name        : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...
dim(auto)
## [1] 397   9
auto$cylinders <- as.factor(auto$cylinders)
# auto$cylinders <-factor(auto$cylinders,levels = c("3","4","5","6","8"))
                   
str(auto)
## 'data.frame':    397 obs. of  9 variables:
##  $ mpg         : num  18 15 18 16 17 15 14 14 14 15 ...
##  $ cylinders   : Factor w/ 5 levels "3","4","5","6",..: 5 5 5 5 5 5 5 5 5 5 ...
##  $ displacement: num  307 350 318 304 302 429 454 440 455 390 ...
##  $ horsepower  : Factor w/ 94 levels "?","100","102",..: 17 35 29 29 24 42 47 46 48 40 ...
##  $ weight      : int  3504 3693 3436 3433 3449 4341 4354 4312 4425 3850 ...
##  $ acceleration: num  12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
##  $ year        : int  70 70 70 70 70 70 70 70 70 70 ...
##  $ origin      : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ name        : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...
table(auto$cylinders)
## 
##   3   4   5   6   8 
##   4 203   3  84 103
aggregate(auto$mpg~auto$cylinders+auto$horsepower,mean,data = auto)
##     auto$cylinders auto$horsepower auto$mpg
## 1                4               ? 31.00000
## 2                6               ? 21.00000
## 3                3             100 23.70000
## 4                4             100 32.90000
## 5                6             100 18.43333
## 6                4             102 20.00000
## 7                5             103 20.30000
## 8                4             105 25.55000
## 9                6             105 18.70000
## 10               8             105 26.60000
## 11               6             107 21.00000
## 12               6             108 19.00000
## 13               3             110 21.50000
## 14               4             110 22.75000
## 15               6             110 19.42500
## 16               8             110 18.96667
## 17               4             112 18.50000
## 18               6             112 22.00000
## 19               4             113 26.00000
## 20               4             115 23.30000
## 21               6             115 25.70000
## 22               6             116 25.40000
## 23               6             120 19.60000
## 24               8             120 15.50000
## 25               6             122 20.00000
## 26               6             125 17.00000
## 27               8             125 21.10000
## 28               8             129 15.30000
## 29               8             130 15.20000
## 30               6             132 32.70000
## 31               6             133 16.20000
## 32               8             135 18.20000
## 33               8             137 14.00000
## 34               8             138 16.50000
## 35               8             139 19.15000
## 36               8             140 16.34286
## 37               8             142 15.50000
## 38               8             145 15.45714
## 39               8             148 14.00000
## 40               8             149 16.00000
## 41               8             150 14.70455
## 42               8             152 14.50000
## 43               8             153 14.00000
## 44               8             155 14.95000
## 45               8             158 13.00000
## 46               8             160 13.00000
## 47               6             165 17.70000
## 48               8             165 14.00000
## 49               8             167 12.00000
## 50               8             170 14.50000
## 51               8             175 13.40000
## 52               8             180 13.50000
## 53               8             190 14.50000
## 54               8             193  9.00000
## 55               8             198 13.50000
## 56               8             200 10.00000
## 57               8             208 11.00000
## 58               8             210 11.00000
## 59               8             215 12.33333
## 60               8             220 14.00000
## 61               8             225 13.33333
## 62               8             230 16.00000
## 63               4              46 26.00000
## 64               4              48 43.60000
## 65               4              49 29.00000
## 66               4              52 34.20000
## 67               4              53 33.00000
## 68               4              54 23.00000
## 69               4              58 37.55000
## 70               4              60 32.16000
## 71               4              61 32.00000
## 72               4              62 33.75000
## 73               4              63 34.40000
## 74               4              64 39.00000
## 75               4              65 35.48000
## 76               4              66 36.10000
## 77               4              67 33.33636
## 78               5              67 36.40000
## 79               4              68 32.18333
## 80               4              69 32.76667
## 81               4              70 32.47500
## 82               4              71 29.02000
## 83               4              72 25.47500
## 84               6              72 15.00000
## 85               4              74 33.53333
## 86               4              75 29.03571
## 87               4              76 31.16667
## 88               6              76 30.70000
## 89               5              77 25.40000
## 90               4              78 28.56000
## 91               6              78 18.00000
## 92               4              79 27.00000
## 93               4              80 28.60000
## 94               4              81 25.00000
## 95               6              81 24.00000
## 96               4              82 31.00000
## 97               4              83 28.12500
## 98               4              84 30.13333
## 99               4              85 24.60000
## 100              6              85 22.90000
## 101              4              86 24.20000
## 102              4              87 23.00000
## 103              4              88 26.75333
## 104              6              88 18.80000
## 105              4              89 25.50000
## 106              3              90 18.00000
## 107              4              90 27.00000
## 108              6              90 20.20000
## 109              8              90 23.90000
## 110              4              91 20.00000
## 111              4              92 27.63333
## 112              4              93 26.00000
## 113              4              94 22.00000
## 114              4              95 24.22857
## 115              6              95 20.00000
## 116              4              96 27.16667
## 117              3              97 19.00000
## 118              4              97 24.42000
## 119              6              97 19.33333
## 120              4              98 22.00000
## 121              6              98 18.50000
summary(auto)
##       mpg        cylinders  displacement     horsepower      weight    
##  Min.   : 9.00   3:  4     Min.   : 68.0   150    : 22   Min.   :1613  
##  1st Qu.:17.50   4:203     1st Qu.:104.0   90     : 20   1st Qu.:2223  
##  Median :23.00   5:  3     Median :146.0   88     : 19   Median :2800  
##  Mean   :23.52   6: 84     Mean   :193.5   110    : 18   Mean   :2970  
##  3rd Qu.:29.00   8:103     3rd Qu.:262.0   100    : 17   3rd Qu.:3609  
##  Max.   :46.60             Max.   :455.0   75     : 14   Max.   :5140  
##                                            (Other):287                 
##   acceleration        year           origin                  name    
##  Min.   : 8.00   Min.   :70.00   Min.   :1.000   ford pinto    :  6  
##  1st Qu.:13.80   1st Qu.:73.00   1st Qu.:1.000   amc matador   :  5  
##  Median :15.50   Median :76.00   Median :1.000   ford maverick :  5  
##  Mean   :15.56   Mean   :75.99   Mean   :1.574   toyota corolla:  5  
##  3rd Qu.:17.10   3rd Qu.:79.00   3rd Qu.:2.000   amc gremlin   :  4  
##  Max.   :24.80   Max.   :82.00   Max.   :3.000   amc hornet    :  4  
##                                                  (Other)       :368
class(auto$cylinders)
## [1] "factor"
plot(auto$cylinders,auto$mpg)

plot(auto$cylinders,auto$mpg,col="red")

plot(auto$cylinders,auto$mpg,col="red",
     varwidth=TRUE)

plot(auto$cylinders,auto$mpg,
     col="red",varwidth=TRUE,
     horizontal=TRUE)

plot(auto$cylinders,auto$mpg,
     col="red",varwidth=TRUE,
     horizontal=TRUE,xlab="MPG",ylab="Cylinders")

hist(auto$mpg)

hist(auto$mpg,col = 2)

hist(auto$mpg,col = 2,breaks=15)

pairs(auto)

pairs(~auto$mpg+auto$displacement+auto$horsepower+
        auto$weight+auto$acceleration,auto)

plot(auto$horsepower,auto$mpg)
identify(auto$horsepower,auto$mpg,auto$name)

## integer(0)
setwd("C:\\Users\\Luis\\Desktop\\ISLR")
college <- read.csv("C:\\Users\\Luis\\Desktop\\ISLR\\college.csv")
#fix(college)
row.names(college)<-college[,1]
#fix(college)
college <- college[,-1]
#fix(college)
summary(college)
##  Private        Apps           Accept          Enroll       Top10perc    
##  No :212   Min.   :   81   Min.   :   72   Min.   :  35   Min.   : 1.00  
##  Yes:565   1st Qu.:  776   1st Qu.:  604   1st Qu.: 242   1st Qu.:15.00  
##            Median : 1558   Median : 1110   Median : 434   Median :23.00  
##            Mean   : 3002   Mean   : 2019   Mean   : 780   Mean   :27.56  
##            3rd Qu.: 3624   3rd Qu.: 2424   3rd Qu.: 902   3rd Qu.:35.00  
##            Max.   :48094   Max.   :26330   Max.   :6392   Max.   :96.00  
##    Top25perc      F.Undergrad     P.Undergrad         Outstate    
##  Min.   :  9.0   Min.   :  139   Min.   :    1.0   Min.   : 2340  
##  1st Qu.: 41.0   1st Qu.:  992   1st Qu.:   95.0   1st Qu.: 7320  
##  Median : 54.0   Median : 1707   Median :  353.0   Median : 9990  
##  Mean   : 55.8   Mean   : 3700   Mean   :  855.3   Mean   :10441  
##  3rd Qu.: 69.0   3rd Qu.: 4005   3rd Qu.:  967.0   3rd Qu.:12925  
##  Max.   :100.0   Max.   :31643   Max.   :21836.0   Max.   :21700  
##    Room.Board       Books           Personal         PhD        
##  Min.   :1780   Min.   :  96.0   Min.   : 250   Min.   :  8.00  
##  1st Qu.:3597   1st Qu.: 470.0   1st Qu.: 850   1st Qu.: 62.00  
##  Median :4200   Median : 500.0   Median :1200   Median : 75.00  
##  Mean   :4358   Mean   : 549.4   Mean   :1341   Mean   : 72.66  
##  3rd Qu.:5050   3rd Qu.: 600.0   3rd Qu.:1700   3rd Qu.: 85.00  
##  Max.   :8124   Max.   :2340.0   Max.   :6800   Max.   :103.00  
##     Terminal       S.F.Ratio      perc.alumni        Expend     
##  Min.   : 24.0   Min.   : 2.50   Min.   : 0.00   Min.   : 3186  
##  1st Qu.: 71.0   1st Qu.:11.50   1st Qu.:13.00   1st Qu.: 6751  
##  Median : 82.0   Median :13.60   Median :21.00   Median : 8377  
##  Mean   : 79.7   Mean   :14.09   Mean   :22.74   Mean   : 9660  
##  3rd Qu.: 92.0   3rd Qu.:16.50   3rd Qu.:31.00   3rd Qu.:10830  
##  Max.   :100.0   Max.   :39.80   Max.   :64.00   Max.   :56233  
##    Grad.Rate     
##  Min.   : 10.00  
##  1st Qu.: 53.00  
##  Median : 65.00  
##  Mean   : 65.46  
##  3rd Qu.: 78.00  
##  Max.   :118.00
pairs(college[,1:10])

names(college)
##  [1] "Private"     "Apps"        "Accept"      "Enroll"      "Top10perc"  
##  [6] "Top25perc"   "F.Undergrad" "P.Undergrad" "Outstate"    "Room.Board" 
## [11] "Books"       "Personal"    "PhD"         "Terminal"    "S.F.Ratio"  
## [16] "perc.alumni" "Expend"      "Grad.Rate"
plot(college$Outstate~college$Private)

college$elite <- rep("No",nrow(college))
#fix(college)
college$elite[college$Top10perc>50]="Yes"
#fix(college)
college$elite <-as.factor(college$elite)
college<-data.frame(college,college$elite)
summary(college)
##  Private        Apps           Accept          Enroll       Top10perc    
##  No :212   Min.   :   81   Min.   :   72   Min.   :  35   Min.   : 1.00  
##  Yes:565   1st Qu.:  776   1st Qu.:  604   1st Qu.: 242   1st Qu.:15.00  
##            Median : 1558   Median : 1110   Median : 434   Median :23.00  
##            Mean   : 3002   Mean   : 2019   Mean   : 780   Mean   :27.56  
##            3rd Qu.: 3624   3rd Qu.: 2424   3rd Qu.: 902   3rd Qu.:35.00  
##            Max.   :48094   Max.   :26330   Max.   :6392   Max.   :96.00  
##    Top25perc      F.Undergrad     P.Undergrad         Outstate    
##  Min.   :  9.0   Min.   :  139   Min.   :    1.0   Min.   : 2340  
##  1st Qu.: 41.0   1st Qu.:  992   1st Qu.:   95.0   1st Qu.: 7320  
##  Median : 54.0   Median : 1707   Median :  353.0   Median : 9990  
##  Mean   : 55.8   Mean   : 3700   Mean   :  855.3   Mean   :10441  
##  3rd Qu.: 69.0   3rd Qu.: 4005   3rd Qu.:  967.0   3rd Qu.:12925  
##  Max.   :100.0   Max.   :31643   Max.   :21836.0   Max.   :21700  
##    Room.Board       Books           Personal         PhD        
##  Min.   :1780   Min.   :  96.0   Min.   : 250   Min.   :  8.00  
##  1st Qu.:3597   1st Qu.: 470.0   1st Qu.: 850   1st Qu.: 62.00  
##  Median :4200   Median : 500.0   Median :1200   Median : 75.00  
##  Mean   :4358   Mean   : 549.4   Mean   :1341   Mean   : 72.66  
##  3rd Qu.:5050   3rd Qu.: 600.0   3rd Qu.:1700   3rd Qu.: 85.00  
##  Max.   :8124   Max.   :2340.0   Max.   :6800   Max.   :103.00  
##     Terminal       S.F.Ratio      perc.alumni        Expend     
##  Min.   : 24.0   Min.   : 2.50   Min.   : 0.00   Min.   : 3186  
##  1st Qu.: 71.0   1st Qu.:11.50   1st Qu.:13.00   1st Qu.: 6751  
##  Median : 82.0   Median :13.60   Median :21.00   Median : 8377  
##  Mean   : 79.7   Mean   :14.09   Mean   :22.74   Mean   : 9660  
##  3rd Qu.: 92.0   3rd Qu.:16.50   3rd Qu.:31.00   3rd Qu.:10830  
##  Max.   :100.0   Max.   :39.80   Max.   :64.00   Max.   :56233  
##    Grad.Rate      elite     college.elite
##  Min.   : 10.00   No :699   No :699      
##  1st Qu.: 53.00   Yes: 78   Yes: 78      
##  Median : 65.00                          
##  Mean   : 65.46                          
##  3rd Qu.: 78.00                          
##  Max.   :118.00
plot(college$Outstate~college$elite)

setwd("C:\\Users\\Luis\\Desktop\\ISLR")
advertising <- read.csv("C:\\Users\\Luis\\Desktop\\ISLR\\Advertising.csv",
                        header = TRUE,sep = ",")
                              
#fix(advertising)
names(advertising)
## [1] "X"         "TV"        "Radio"     "Newspaper" "Sales"
advertising <- advertising[,-1]
#fix(advertising)
attach(advertising)
model_1 <- lm(Sales~TV,data = advertising)
summary(model_1)
## 
## Call:
## lm(formula = Sales ~ TV, data = advertising)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.3860 -1.9545 -0.1913  2.0671  7.2124 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 7.032594   0.457843   15.36   <2e-16 ***
## TV          0.047537   0.002691   17.67   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.259 on 198 degrees of freedom
## Multiple R-squared:  0.6119, Adjusted R-squared:  0.6099 
## F-statistic: 312.1 on 1 and 198 DF,  p-value: < 2.2e-16
plot(model_1)

# 3.6.2 Simple Linear Regression
library(MASS)
objects(grep("MASS",search()))
##   [1] "abbey"             "accdeaths"         "addterm"          
##   [4] "Aids2"             "Animals"           "anorexia"         
##   [7] "area"              "as.fractions"      "bacteria"         
##  [10] "bandwidth.nrd"     "bcv"               "beav1"            
##  [13] "beav2"             "biopsy"            "birthwt"          
##  [16] "Boston"            "boxcox"            "cabbages"         
##  [19] "caith"             "Cars93"            "cats"             
##  [22] "cement"            "chem"              "con2tr"           
##  [25] "contr.sdif"        "coop"              "corresp"          
##  [28] "cov.mcd"           "cov.mve"           "cov.rob"          
##  [31] "cov.trob"          "cpus"              "crabs"            
##  [34] "Cushings"          "DDT"               "deaths"           
##  [37] "denumerate"        "dose.p"            "drivers"          
##  [40] "dropterm"          "eagles"            "enlist"           
##  [43] "epil"              "eqscplot"          "farms"            
##  [46] "fbeta"             "fgl"               "fitdistr"         
##  [49] "forbes"            "fractions"         "frequency.polygon"
##  [52] "GAGurine"          "galaxies"          "gamma.dispersion" 
##  [55] "gamma.shape"       "gehan"             "genotype"         
##  [58] "geyser"            "gilgais"           "ginv"             
##  [61] "glm.convert"       "glm.nb"            "glmmPQL"          
##  [64] "hills"             "hist.FD"           "hist.scott"       
##  [67] "housing"           "huber"             "hubers"           
##  [70] "immer"             "Insurance"         "is.fractions"     
##  [73] "isoMDS"            "kde2d"             "lda"              
##  [76] "ldahist"           "leuk"              "lm.gls"           
##  [79] "lm.ridge"          "lmsreg"            "lmwork"           
##  [82] "loglm"             "loglm1"            "logtrans"         
##  [85] "lqs"               "lqs.formula"       "ltsreg"           
##  [88] "mammals"           "mca"               "mcycle"           
##  [91] "Melanoma"          "menarche"          "michelson"        
##  [94] "minn38"            "motors"            "muscle"           
##  [97] "mvrnorm"           "nclass.freq"       "neg.bin"          
## [100] "negative.binomial" "negexp.SSival"     "newcomb"          
## [103] "nlschools"         "npk"               "npr1"             
## [106] "Null"              "oats"              "OME"              
## [109] "painters"          "parcoord"          "petrol"           
## [112] "phones"            "Pima.te"           "Pima.tr"          
## [115] "Pima.tr2"          "polr"              "psi.bisquare"     
## [118] "psi.hampel"        "psi.huber"         "qda"              
## [121] "quine"             "Rabbit"            "rational"         
## [124] "renumerate"        "rlm"               "rms.curv"         
## [127] "rnegbin"           "road"              "rotifer"          
## [130] "Rubber"            "sammon"            "select"           
## [133] "Shepard"           "ships"             "shoes"            
## [136] "shrimp"            "shuttle"           "Sitka"            
## [139] "Sitka89"           "Skye"              "snails"           
## [142] "SP500"             "stdres"            "steam"            
## [145] "stepAIC"           "stormer"           "studres"          
## [148] "survey"            "synth.te"          "synth.tr"         
## [151] "theta.md"          "theta.ml"          "theta.mm"         
## [154] "topo"              "Traffic"           "truehist"         
## [157] "ucv"               "UScereal"          "UScrime"          
## [160] "VA"                "waders"            "whiteside"        
## [163] "width.SJ"          "write.matrix"      "wtloss"
data("Boston")
Boston[1:5,]
##      crim zn indus chas   nox    rm  age    dis rad tax ptratio  black
## 1 0.00632 18  2.31    0 0.538 6.575 65.2 4.0900   1 296    15.3 396.90
## 2 0.02731  0  7.07    0 0.469 6.421 78.9 4.9671   2 242    17.8 396.90
## 3 0.02729  0  7.07    0 0.469 7.185 61.1 4.9671   2 242    17.8 392.83
## 4 0.03237  0  2.18    0 0.458 6.998 45.8 6.0622   3 222    18.7 394.63
## 5 0.06905  0  2.18    0 0.458 7.147 54.2 6.0622   3 222    18.7 396.90
##   lstat medv
## 1  4.98 24.0
## 2  9.14 21.6
## 3  4.03 34.7
## 4  2.94 33.4
## 5  5.33 36.2
str(Boston)
## '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   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 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    : int  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 ...
##  $ black  : 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 ...
lm.fit <- lm(Boston$medv~Boston$lstat,data = Boston)
lm.fit
## 
## Call:
## lm(formula = Boston$medv ~ Boston$lstat, data = Boston)
## 
## Coefficients:
##  (Intercept)  Boston$lstat  
##        34.55         -0.95
summary(lm.fit)
## 
## Call:
## lm(formula = Boston$medv ~ Boston$lstat, data = Boston)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -15.168  -3.990  -1.318   2.034  24.500 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  34.55384    0.56263   61.41   <2e-16 ***
## Boston$lstat -0.95005    0.03873  -24.53   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.216 on 504 degrees of freedom
## Multiple R-squared:  0.5441, Adjusted R-squared:  0.5432 
## F-statistic: 601.6 on 1 and 504 DF,  p-value: < 2.2e-16
names(lm.fit)
##  [1] "coefficients"  "residuals"     "effects"       "rank"         
##  [5] "fitted.values" "assign"        "qr"            "df.residual"  
##  [9] "xlevels"       "call"          "terms"         "model"
coefficients(lm.fit)
##  (Intercept) Boston$lstat 
##   34.5538409   -0.9500494
confint(lm.fit)
##                  2.5 %     97.5 %
## (Intercept)  33.448457 35.6592247
## Boston$lstat -1.026148 -0.8739505
predict(lm.fit,data.frame(lstat=(c(5,10,15))),
interval="confidence")
## Warning: 'newdata' had 3 rows but variables found have 506 rows
##            fit        lwr        upr
## 1   29.8225951 29.0252990 30.6198912
## 2   25.8703898 25.2652456 26.4755340
## 3   30.7251420 29.8734766 31.5768074
## 4   31.7606958 30.8435939 32.6777976
## 5   29.4900778 28.7120765 30.2680791
## 6   29.6040837 28.8195155 30.3886520
## 7   22.7447274 22.2015728 23.2878820
## 8   16.3603958 15.6261142 17.0946773
## 9    6.1188637  4.6964329  7.5412945
## 10  18.3079969 17.6682721 18.9477218
## 11  15.1253316 14.3211061 15.9295571
## 12  21.9466860 21.4017704 22.4916015
## 13  19.6285655 19.0379343 20.2191967
## 14  26.7064332 26.0688676 27.3439989
## 15  24.8063345 24.2337154 25.3789536
## 16  26.5069229 25.8775896 27.1362562
## 17  28.3025161 27.5895545 29.0154778
## 18  20.6166169 20.0524476 21.1807861
## 19  23.4477639 22.8999502 23.9955777
## 20  23.8372842 23.2844310 24.3901373
## 21  14.5838035 13.7470635 15.4205434
## 22  21.4146583 20.8644307 21.9648859
## 23  16.7689170 16.0562575 17.4815765
## 24  15.6668597 14.8940813 16.4396381
## 25  19.0680364 18.4583230 19.6777498
## 26  18.8685260 18.2513745 19.4856776
## 27  20.4836100 19.9164496 21.0507703
## 28  18.1369880 17.4899128 18.7840633
## 29  22.3932092 21.8502047 22.9362136
## 30  23.1722496 22.6269495 23.7175497
## 31  13.0827255 12.1512199 14.0142311
## 32  22.1651973 21.6215101 22.7088845
## 33   8.2279733  6.9600530  9.4958936
## 34  17.1204352 16.4256854 17.8151851
## 35  15.2298370 14.4317674 16.0279067
## 36  25.3573631 24.7692166 25.9455097
## 37  23.7137778 23.1627087 24.2648468
## 38  26.2219080 25.6038085 26.8400076
## 39  24.9298409 24.3539993 25.5056826
## 40  30.4496277 29.6148475 31.2844078
## 41  32.6727432 31.6958039 33.6496824
## 42  29.9556020 29.1504707 30.7607333
## 43  29.0340541 28.2817852 29.7863231
## 44  27.4854737 26.8130853 28.1578620
## 45  25.4808696 24.8888470 26.0728921
## 46  24.8538370 24.2799965 25.4276775
## 47  21.1106425 20.5559305 21.6653546
## 48  16.6929130 15.9762946 17.4095315
## 49   5.2828203  3.7982718  6.7673688
## 50  19.1630413 18.5567540 19.7693287
## 51  21.7756771 21.2294109 22.3219432
## 52  25.5948755 24.9991517 26.1905993
## 53  29.5375803 28.7568490 30.3183116
## 54  26.5449248 25.9140464 27.1758033
## 55  20.4931104 19.9261699 21.0600510
## 56  29.9841035 29.1772848 30.7909222
## 57  29.0720561 28.3176769 29.8264354
## 58  30.8011459 29.9447810 31.6575109
## 59  28.0365023 27.3371644 28.7358402
## 60  25.7943858 25.1919064 26.3968653
## 61  22.0606919 21.5164871 22.6048967
## 62  20.8351282 20.2754673 21.3947892
## 63  28.1600087 27.4543928 28.8656246
## 64  25.5283720 24.9348216 26.1219225
## 65  26.9059436 26.2598551 27.5520321
## 66  30.1171104 29.3023789 30.9318418
## 67  24.8253355 24.2522306 25.3984404
## 68  26.8584411 26.2144076 27.5024746
## 69  22.1176948 21.5737883 22.6616014
## 70  26.2029071 25.5855337 26.8202804
## 71  28.1695092 27.4634070 28.8756115
## 72  25.1673533 24.5848920 25.7498145
## 73  29.3095684 28.5418575 30.0772794
## 74  27.3904688 26.7225420 28.0583955
## 75  28.1125063 27.4093148 28.8156977
## 76  26.0603997 25.4483794 26.6724199
## 77  23.1817501 22.6363780 23.7271222
## 78  24.7968340 24.2244565 25.3692115
## 79  22.8302319 22.2868201 23.3736436
## 80  25.9083918 25.3018967 26.5148869
## 81  29.5280798 28.7478952 30.3082644
## 82  27.6944845 27.0120847 28.3768844
## 83  28.1695092 27.4634070 28.8756115
## 84  27.4189702 26.7497109 28.0882295
## 85  25.4143661 24.8244481 26.0042841
## 86  28.3500186 27.6345847 29.0654525
## 87  22.3362062 21.7930886 22.8793238
## 88  26.5354243 25.9049332 27.1659155
## 89  29.3285694 28.5597816 30.0973572
## 90  29.1385596 28.3804719 29.8966472
## 91  26.1839061 25.5672560 26.8005562
## 92  26.7634362 26.1234646 27.4034078
## 93  26.8014382 26.1598495 27.4430268
## 94  28.6540344 27.9225070 29.3855618
## 95  24.4928182 23.9276839 25.0579525
## 96  28.2360127 27.5264925 28.9455329
## 97  23.7802812 23.2282728 24.3322896
## 98  30.5541331 29.7129765 31.3952897
## 99  31.1621647 30.2832444 32.0410850
## 100 28.6730354 27.9404870 29.4055838
## 101 25.6043760 25.0083385 26.2004135
## 102 27.2669623 26.6047498 27.9291749
## 103 24.4548162 23.8905200 25.0191125
## 104 21.7851776 21.2389954 22.3313598
## 105 22.8397323 22.2962867 23.3831780
## 106 18.9065280 18.2908184 19.5222377
## 107 16.8259199 16.1162099 17.5356300
## 108 21.1676455 20.6138531 21.7214379
## 109 22.8967353 22.3530640 23.4404067
## 110 19.7805734 19.1946310 20.3665159
## 111 22.2031993 21.6596684 22.7467302
## 112 24.9013394 24.3262550 25.4764239
## 113 19.1535409 18.5469143 19.7601674
## 114 18.3174974 17.6781748 18.9568201
## 115 24.6258251 24.0576390 25.1940113
## 116 19.5810631 18.9889229 20.1732032
## 117 23.1152467 22.5703565 23.6601369
## 118 24.7683325 24.1966742 25.3399908
## 119 19.9515823 19.3706548 20.5325098
## 120 21.6236692 21.0759176 22.1714208
## 121 20.9016317 20.3432411 21.4600222
## 122 20.9966366 20.4399775 21.5532957
## 123 17.5194560 16.8441884 18.1947235
## 124 10.4130868  9.3008106 11.5253630
## 125 17.8519732 17.1921972 18.5117493
## 126 20.4836100 19.9164496 21.0507703
## 127  8.6554955  7.4184349  9.8925561
## 128 18.2224925 17.5791184 18.8658666
## 129 19.9325813 19.3511104 20.5140522
## 130 17.1299357 16.4356605 17.8242109
## 131 22.5832190 22.0403147 23.1261233
## 132 22.9062358 22.3625231 23.4499485
## 133 23.9892921 23.4340089 24.5445752
## 134 20.2745991 19.7023691 20.8468291
## 135 18.1084866 17.4601662 18.7568069
## 136 18.4410038 17.8068503 19.0751574
## 137 18.4980068 17.8662010 19.1298126
## 138 20.6926208 20.1300773 21.2551643
## 139 14.2987887 13.4445481 15.1530292
## 140 17.0159298 16.3159259 17.7159337
## 141 11.6006485 10.5703489 12.6309481
## 142  1.8626426  0.1202312  3.6050541
## 143  9.0735172  7.8664540 10.2805804
## 144  9.4535370  8.2735814 10.6334925
## 145  6.7268953  5.3493556  8.1044350
## 146  8.1424688  6.8683558  9.4165819
## 147 18.7355191 18.1132302 19.3578080
## 148  6.4988835  5.1045396  7.8932273
## 149  7.6484432  6.3384222  8.9584642
## 150 14.1752822 13.3133807 15.0371838
## 151 21.1581450 20.6042018 21.7120882
## 152 21.9371855 21.3922039 22.4821671
## 153 23.0392427 22.4948400 23.5836454
## 154 19.5525616 18.9595062 20.1456170
## 155 20.1890947 19.6146630 20.7635263
## 156 20.2840996 19.7121097 20.8560895
## 157 19.2200443 18.6157758 19.8243128
## 158 30.1931143 29.3738334 31.0123953
## 159 28.4450235 27.7246102 29.1654369
## 160 27.5329762 26.8583359 28.2076164
## 161 29.3285694 28.5597816 30.0973572
## 162 32.9102555 31.9174432 33.9030678
## 163 32.7297461 31.7490076 33.7104847
## 164 31.3996770 30.5057178 32.2936362
## 165 23.4952664 22.9469306 24.0436022
## 166 25.2338567 24.6494444 25.8182690
## 167 31.0386583 30.1674966 31.9098200
## 168 23.0202417 22.4759503 23.5645332
## 169 24.0082931 23.4526882 24.5638979
## 170 23.7992822 23.2469962 24.3515682
## 171 20.8446287 20.2851522 21.4041052
## 172 23.1247472 22.5797913 23.6697030
## 173 20.5976159 20.0330307 21.1622011
## 174 25.9653947 25.3568503 26.5739391
## 175 25.3953651 24.8060409 25.9846893
## 176 29.4900778 28.7120765 30.2680791
## 177 24.9488419 24.3724910 25.5251928
## 178 28.5780304 27.8505694 29.3054914
## 179 27.9794994 27.2830307 28.6759680
## 180 29.7655921 28.9716338 30.5595504
## 181 27.3714678 26.7044264 28.0385091
## 182 25.5758745 24.9807757 26.1709733
## 183 29.9746030 29.1683471 30.7808589
## 184 29.1575606 28.3984099 29.9167112
## 185 21.2721509 20.7199504 21.8243514
## 186 22.0606919 21.5164871 22.6048967
## 187 30.3261213 29.4988312 31.1534114
## 188 28.2075112 27.4994587 28.9155637
## 189 30.2216158 29.4006237 31.0426079
## 190 29.4330749 28.6583375 30.2078122
## 191 29.7085892 28.9179564 30.4992219
## 192 30.0981094 29.2845122 30.9117066
## 193 31.8271992 30.9057987 32.7485998
## 194 29.7750926 28.9805789 30.5696064
## 195 30.3926247 29.5613078 31.2239417
## 196 31.7321943 30.8169314 32.6474571
## 197 30.6776395 29.8289023 31.5263767
## 198 26.3739159 25.7499038 26.9979281
## 199 28.2645142 27.5535219 28.9755064
## 200 30.2216158 29.4006237 31.0426079
## 201 30.3261213 29.4988312 31.1534114
## 202 27.4949742 26.8221366 28.1678118
## 203 31.5991874 30.6924798 32.5058950
## 204 30.9341528 30.0695221 31.7987836
## 205 31.8176987 30.8969129 32.7384845
## 206 24.2268044 23.6672150 24.7863938
## 207 24.1317995 23.5740064 24.6895925
## 208 17.3959496 16.7147525 18.0771466
## 209 20.6261174 20.0621547 21.1900800
## 210 12.6172013 11.6551493 13.5792533
## 211 18.1464885 17.4998271 18.7931500
## 212 11.7716574 10.7529741 12.7903406
## 213 19.3245497 18.7239101 19.9251894
## 214 25.6423779 25.0450775 26.2396784
## 215  6.4798825  5.0841366  7.8756284
## 216 25.5568735 24.9623965 26.1513505
## 217 21.7186741 21.1718822 22.2654660
## 218 25.3478626 24.7600084 25.9357169
## 219 17.5289565 16.8541412 18.2037718
## 220 24.5783227 24.0112476 25.1453978
## 221 25.3288617 24.7415895 25.9161338
## 222 14.1657817 13.3032890 15.0282745
## 223 25.1198508 24.5387572 25.7009444
## 224 27.3334658 26.6681885 27.9987431
## 225 30.6206366 29.7754040 31.4658691
## 226 30.1551124 29.3381087 30.9721161
## 227 31.5801864 30.6746974 32.4856754
## 228 28.5115270 27.7876008 29.2354531
## 229 30.8296474 29.9715157 31.6877792
## 230 30.9816553 30.1140599 31.8492507
## 231 23.4857659 22.9375366 24.0339952
## 232 29.5660818 28.7837082 30.3484554
## 233 32.2072190 31.2610537 33.1533843
## 234 30.8011459 29.9447810 31.6575109
## 235 26.9059436 26.2598551 27.5520321
## 236 24.2173039 23.6578985 24.7767093
## 237 25.4903700 24.8980436 26.0826965
## 238 30.0601074 29.2487749 30.8714400
## 239 28.5115270 27.7876008 29.2354531
## 240 27.5519771 26.8764322 28.2275221
## 241 23.7422792 23.1908137 24.2937448
## 242 22.7732289 22.2299981 23.3164596
## 243 23.8942871 23.3405528 24.4480214
## 244 29.6230847 28.8374170 30.4087525
## 245 22.6782240 22.1352097 23.2212382
## 246 17.0159298 16.3159259 17.7159337
## 247 25.8513888 25.2469154 26.4558622
## 248 24.9108399 24.3355040 25.4861759
## 249 25.5093710 24.9164342 26.1023078
## 250 28.3215171 27.6075680 29.0354662
## 251 28.9485497 28.2010055 29.6960939
## 252 31.1431637 30.2654398 32.0208876
## 253 31.2001667 30.3188505 32.0814828
## 254 31.1906662 30.3099493 32.0713830
## 255 28.3120166 27.5985615 29.0254718
## 256 25.7658844 25.1643914 26.3673773
## 257 31.5991874 30.6924798 32.5058950
## 258 29.6895882 28.9000612 30.4791152
## 259 27.1529564 26.4959304 27.8099824
## 260 27.9985003 27.3010773 28.6959234
## 261 25.4428676 24.8520526 26.0336825
## 262 27.6564826 26.9759227 28.3370425
## 263 28.9390492 28.1920279 29.6860705
## 264 23.8657856 23.3124965 24.4190748
## 265 26.8584411 26.2144076 27.5024746
## 266 24.6258251 24.0576390 25.1940113
## 267 20.5026109 19.9358892 21.0693327
## 268 27.4854737 26.8130853 28.1578620
## 269 31.5516849 30.6480220 32.4553478
## 270 21.5856672 21.0375026 22.1338318
## 271 22.2031993 21.6596684 22.7467302
## 272 28.2930156 27.5805471 29.0054842
## 273 27.2099594 26.5503508 27.8695680
## 274 28.3025161 27.5895545 29.0154778
## 275 31.2001667 30.3188505 32.0814828
## 276 31.7226938 30.8080435 32.6373441
## 277 28.8060423 28.0662988 29.5457858
## 278 30.6016356 29.7575690 31.4457021
## 279 27.7229860 27.0392006 28.4067715
## 280 29.9461015 29.1415320 30.7506710
## 281 30.9816553 30.1140599 31.8492507
## 282 30.1931143 29.3738334 31.0123953
## 283 31.6941923 30.7813783 32.6070063
## 284 31.5516849 30.6480220 32.4553478
## 285 27.0959535 26.4414884 27.7504186
## 286 26.7349347 26.0961690 27.3737004
## 287 22.2697027 21.7264045 22.8130009
## 288 27.7704885 27.0843830 28.4565940
## 289 27.3334658 26.6681885 27.9987431
## 290 25.5188715 24.9256283 26.1121147
## 291 31.3901765 30.4968218 32.2835312
## 292 31.1716652 30.2921463 32.0511841
## 293 30.0886089 29.2755783 30.9016395
## 294 26.4024174 25.7772765 27.0275583
## 295 24.6733276 24.1040072 25.2426480
## 296 28.5970314 27.8685565 29.3255064
## 297 27.5329762 26.8583359 28.2076164
## 298 19.5050591 18.9104619 20.0996563
## 299 29.8320956 29.0342420 30.6299492
## 300 30.0506069 29.2398397 30.8613742
## 301 28.7870413 28.0483308 29.5257518
## 302 25.5283720 24.9348216 26.1219225
## 303 26.3169130 25.6951394 26.9386865
## 304 29.9366010 29.1325930 30.7406091
## 305 27.9699989 27.2740067 28.6659911
## 306 26.0699002 25.4575282 26.6822721
## 307 28.4070216 27.6886055 29.1254376
## 308 27.3999692 26.7315989 28.0683396
## 309 30.2406168 29.4184824 31.0627513
## 310 25.0818488 24.5018338 25.6618639
## 311 22.5452170 22.0023268 23.0881072
## 312 28.8725457 28.1291735 29.6159179
## 313 23.4192625 22.8717494 23.9667755
## 314 27.0484510 26.3961032 27.7007987
## 315 25.7373829 25.1368693 26.3378965
## 316 23.6282733 23.0783385 24.1782081
## 317 17.1394362 16.4456351 17.8332373
## 318 19.4100542 18.8123127 20.0077956
## 319 24.7113296 24.1410850 25.2815741
## 320 22.4597126 21.9167917 23.0026335
## 321 27.7134855 27.0301625 28.3968086
## 322 28.0270018 27.3281434 28.7258602
## 323 27.2384609 26.5775529 27.8993688
## 324 23.4002615 22.8529438 23.9475792
## 325 28.7395388 28.0034034 29.4756743
## 326 29.7275902 28.9358503 30.5193301
## 327 28.7110374 27.9764418 29.4456329
## 328 22.4027096 21.8597204 22.9456989
## 329 25.0818488 24.5018338 25.6618639
## 330 27.5804786 26.9035726 28.2573847
## 331 25.9178923 25.3110575 26.5247270
## 332 22.7447274 22.2015728 23.2878820
## 333 27.1149544 26.4596381 27.7702708
## 334 29.1575606 28.3984099 29.9167112
## 335 28.1410077 27.4363631 28.8456524
## 336 26.9439456 26.2962017 27.5916894
## 337 25.2433572 24.6586627 25.8280517
## 338 24.5213197 23.9555470 25.0870925
## 339 26.4689209 25.8411218 27.0967200
## 340 25.3003602 24.7139549 25.8867654
## 341 25.7278824 25.1276937 26.3280711
## 342 29.3380699 28.5687431 30.1073967
## 343 26.3359140 25.7133971 26.9584309
## 344 27.7324865 27.0482381 28.4167349
## 345 30.1741134 29.3559717 30.9922550
## 346 24.5498212 23.9834015 25.1162409
## 347 22.5167156 21.9738247 23.0596064
## 348 28.5115270 27.7876008 29.2354531
## 349 28.8630453 28.1201927 29.6058978
## 350 28.9580502 28.2099826 29.7061177
## 351 28.8725457 28.1291735 29.6159179
## 352 29.3380699 28.5687431 30.1073967
## 353 27.1529564 26.4959304 27.8099824
## 354 30.2786188 29.4541960 31.1030416
## 355 26.9059436 26.2598551 27.5520321
## 356 29.2620660 28.4970407 30.0270913
## 357 17.8329723 17.1723301 18.4936144
## 358 21.9466860 21.4017704 22.4916015
## 359 23.6472743 23.0970946 24.1974540
## 360 22.5167156 21.9738247 23.0596064
## 361 27.1529564 26.4959304 27.8099824
## 362 21.0726405 20.5172954 21.6279857
## 363 24.8728380 24.2985026 25.4471734
## 364 20.6451183 20.0815659 21.2086707
## 365 29.5280798 28.7478952 30.3082644
## 366 27.7894895 27.1024523 28.4765267
## 367 21.2531499 20.7006691 21.8056308
## 368 21.8896830 21.3443551 22.4350109
## 369 31.4566800 30.5590890 32.3542710
## 370 31.0101568 30.1407794 31.8795342
## 371 31.7416948 30.8258192 32.6575704
## 372 25.4998705 24.9072393 26.0925018
## 373 26.1174026 25.5032610 26.7315443
## 374  1.5206248 -0.2478391  3.2890888
## 375 -1.5195331 -3.5211516  0.4820854
## 376 21.7851776 21.2389954 22.3313598
## 377 12.4746939 11.5031968 13.4461910
## 378 14.3747926 13.5252436 15.2243416
## 379 12.0471717 11.0470929 13.0472505
## 380 13.8617660 12.9802176 14.7433144
## 381 18.2034915 17.5592994 18.8476836
## 382 14.5268005 13.6865809 15.3670201
## 383 12.1326761 11.1383423 13.1270100
## 384 11.2206288 10.1643364 12.2769211
## 385  5.4538292  3.9820212  6.9256372
## 386  5.2828203  3.7982718  6.7673688
## 387  7.6864452  6.3791938  8.9936965
## 388  4.1617621  2.5932928  5.7302313
## 389  5.4633297  3.9922290  6.9344304
## 390 14.7453119 13.9183735 15.5722502
## 391 18.2984964 17.6583687 18.9386242
## 392 16.7309150 16.0162798 17.4455502
## 393 10.1565735  9.0263197 11.2868272
## 394 20.1415922 19.5659058 20.7172786
## 395 19.0205339 18.4090793 19.6319886
## 396 18.2889959 17.6484647 18.9295272
## 397 16.1513849 15.4057280 16.8970418
## 398 15.6288578 14.8539101 16.4038054
## 399  5.4918311  4.0228520  6.9608103
## 400  6.0808617  4.6556170  7.5061065
## 401  9.1210197  7.9173537 10.3246857
## 402 15.2488380 14.4518832 16.0457928
## 403 15.2583385 14.4619407 16.0547363
## 404 15.7713652 15.0045215 16.5382088
## 405  8.5414896  7.2962170  9.7867621
## 406 12.7217067 11.7665537 13.6768597
## 407 12.3796890 11.4018718 13.3575061
## 408 23.0297422 22.4853957 23.5740888
## 409  9.4725379  8.2939335 10.6511424
## 410 15.7618647 14.9944833 16.5292460
## 411 24.9488419 24.3724910 25.5251928
## 412 14.3937936 13.5454147 15.2421725
## 413  1.9006446  0.1611253  3.6401639
## 414 15.4768499 14.6931671 16.2605326
## 415 -0.5789842 -2.5081970  1.3502286
## 416  6.9549072  5.5941340  8.3156803
## 417 10.0520680  8.9144652 11.1896709
## 418  9.2445261  8.0496812 10.4393710
## 419 14.9638232 14.1500064 15.7776400
## 420 12.9497186 12.0095351 13.8899021
## 421 20.2840996 19.7121097 20.8560895
## 422 19.6380660 19.0477342 20.2283979
## 423 21.1581450 20.6042018 21.7120882
## 424 12.4271914 11.4525366 13.4018463
## 425 18.2509940 17.6088421 18.8931459
## 426 11.3821371 10.3369210 12.4273532
## 427 19.6475665 19.0575331 20.2375999
## 428 20.7591243 20.1979530 21.3202955
## 429 14.1087788 13.2427333 14.9748243
## 430 11.6766524 10.6515220 12.7017829
## 431 17.7949703 17.1325889 18.4573517
## 432 15.8473691 15.0848129 16.6099253
## 433 23.1247472 22.5797913 23.6697030
## 434 19.1440404 18.5370739 19.7510069
## 435 20.1415922 19.5659058 20.7172786
## 436 12.4461924 11.4728012 13.4195836
## 437 17.4054500 16.7247123 18.0861877
## 438  9.4250355  8.2430525 10.6070185
## 439  2.2331619  0.5189267  3.9473970
## 440 12.8167117 11.8678100 13.7656134
## 441 13.5482497 12.6467830 14.4497163
## 442 16.0088775 15.2553498 16.7624052
## 443 18.7925221 18.1724521 19.4125921
## 444 16.6454106 15.9263026 17.3645185
## 445 11.9521668 10.9456884 12.9586451
## 446 11.7716574 10.7529741 12.7903406
## 447 17.6524629 16.9834761 18.3214496
## 448 18.9350295 18.3203935 19.5496655
## 449 17.3294461 16.6450182 18.0138740
## 450 16.2083879 15.4658534 16.9509223
## 451 17.9849801 17.3311996 18.6387607
## 452 17.7094658 17.0431368 18.3757948
## 453 18.1464885 17.4998271 18.7931500
## 454 18.6500147 18.0243497 19.2756797
## 455 16.7784175 16.0662507 17.4905842
## 456 17.3294461 16.6450182 18.0138740
## 457 16.4934027 15.7662519 17.2205534
## 458 18.4600048 17.8266365 19.0933731
## 459 19.1345399 18.5272327 19.7418471
## 460 20.5881154 20.0233208 21.1529100
## 461 18.9540305 18.3401067 19.5679543
## 462 20.6356178 20.0718608 21.1993749
## 463 21.2626504 20.7103102 21.8149906
## 464 24.7778330 24.2059359 25.3497301
## 465 21.9941884 21.4495875 22.5387893
## 466 21.1296435 20.5752420 21.6840450
## 467 18.2604945 17.6187487 18.9022402
## 468 14.2987887 13.4445481 15.1530292
## 469 17.3294461 16.6450182 18.0138740
## 470 20.5311124 19.9650415 21.0971833
## 471 19.0775369 18.4681695 19.6869043
## 472 22.3267057 21.7835655 22.8698459
## 473 20.9111322 20.3529192 21.4693451
## 474 23.4762654 22.9281416 24.0243893
## 475 17.3199456 16.6350541 18.0048371
## 476 11.6576515 10.6312297 12.6840732
## 477 16.8069190 16.0962277 17.5176102
## 478 10.8881115  9.8088847 11.9673382
## 479 17.4244510 16.7446305 18.1042716
## 480 22.0986939 21.5546921 22.6426956
## 481 24.3503108 23.7882407 24.9123810
## 482 27.2004589 26.5412822 27.8596355
## 483 27.8939949 27.2017959 28.5861939
## 484 24.6543266 24.0854627 25.2231906
## 485 21.8801825 21.3347822 22.4255828
## 486 24.5023187 23.9369726 25.0676649
## 487 20.3221016 19.7510629 20.8931402
## 488 23.6757758 23.1252211 24.2263305
## 489 17.3959496 16.7147525 18.0771466
## 490 11.7811579 10.7631185 12.7991973
## 491  6.3563761  4.9515111  7.7612410
## 492 17.3864491 16.7047920 18.0681061
## 493 21.8706820 21.3252083 22.4161558
## 494 23.1437481 22.5986577 23.6888385
## 495 21.6426702 21.0951188 22.1902215
## 496 17.8329723 17.1723301 18.4936144
## 497 14.4697975 13.6260879 15.3135072
## 498 21.1581450 20.6042018 21.7120882
## 499 22.2792032 21.7359340 22.8224724
## 500 20.2080956 19.6341596 20.7820317
## 501 20.9396336 20.3819475 21.4973198
## 502 25.3668636 24.7784239 25.9553033
## 503 25.9273927 25.3202176 26.5345679
## 504 29.1955625 28.4342810 29.9568440
## 505 28.3975211 27.6796032 29.1154389
## 506 27.0674520 26.4142591 27.7206448
predict(lm.fit,data.frame(lstat=(c(5,10,15))),
        interval="prediction")
## Warning: 'newdata' had 3 rows but variables found have 506 rows
##            fit         lwr      upr
## 1   29.8225951  17.5846032 42.06059
## 2   25.8703898  13.6434129 38.09737
## 3   30.7251420  18.4834878 42.96680
## 4   31.7606958  19.5143151 44.00708
## 5   29.4900778  17.2533279 41.72683
## 6   29.6040837  17.3669145 41.84125
## 7   22.7447274  10.5206618 34.96879
## 8   16.3603958   4.1263477 28.59444
## 9    6.1188637  -6.1756909 18.41342
## 10  18.3079969   6.0792598 30.53673
## 11  15.1253316   2.8868863 27.36378
## 12  21.9466860   9.7225420 34.17083
## 13  19.6285655   7.4022984 31.85483
## 14  26.7064332  14.4778089 38.93506
## 15  24.8063345  12.5809243 37.03174
## 16  26.5069229  14.2787250 38.73512
## 17  28.3025161  16.0697291 40.53530
## 18  20.6166169   8.3915995 32.84163
## 19  23.4477639  11.2234904 35.67204
## 20  23.8372842  11.6127838 36.06178
## 21  14.5838035   2.3431786 26.82443
## 22  21.4146583   9.1902764 33.63904
## 23  16.7689170   4.5361476 29.00169
## 24  15.6668597   3.4304407 27.90328
## 25  19.0680364   6.8408326 31.29524
## 26  18.8685260   6.6409490 31.09610
## 27  20.4836100   8.2584542 32.70877
## 28  18.1369880   5.9078642 30.36611
## 29  22.3932092  10.1691502 34.61727
## 30  23.1722496  10.9480885 35.39641
## 31  13.0827255   0.8352577 25.33019
## 32  22.1651973   9.9411080 34.38929
## 33   8.2279733  -4.0496643 20.50561
## 34  17.1204352   4.8886962 29.35217
## 35  15.2298370   2.9917948 27.46788
## 36  25.3573631  13.1312158 37.58351
## 37  23.7137778  11.4893579 35.93820
## 38  26.2219080  13.9942832 38.44953
## 39  24.9298409  12.7042793 37.15540
## 40  30.4496277  18.2091366 42.69012
## 41  32.6727432  20.4217361 44.92375
## 42  29.9556020  17.7170972 42.19411
## 43  29.0340541  16.7989133 41.26919
## 44  27.4854737  15.2549843 39.71596
## 45  25.4808696  13.2545351 37.70720
## 46  24.8538370  12.6283694 37.07930
## 47  21.1106425   8.8860579 33.33523
## 48  16.6929130   4.4599124 28.92591
## 49   5.2828203  -7.0190758 17.58472
## 50  19.1630413   6.9360079 31.39007
## 51  21.7756771   9.5514728 33.99988
## 52  25.5948755  13.3683612 37.82139
## 53  29.5375803  17.3006565 41.77450
## 54  26.5449248  14.3166473 38.77320
## 55  20.4931104   8.2679648 32.71826
## 56  29.9841035  17.7454876 42.22272
## 57  29.0720561  16.8367853 41.30733
## 58  30.8011459  18.5591639 43.04313
## 59  28.0365023  15.8045018 40.26850
## 60  25.7943858  13.5675406 38.02123
## 61  22.0606919   9.8365795 34.28480
## 62  20.8351282   8.6103180 33.05994
## 63  28.1600087  15.9276477 40.39237
## 64  25.5283720  13.3019635 37.75478
## 65  26.9059436  14.6768719 39.13502
## 66  30.1171104  17.8779703 42.35625
## 67  24.8253355  12.5999025 37.05077
## 68  26.8584411  14.6294778 39.08740
## 69  22.1176948   9.8935958 34.34179
## 70  26.2029071  13.9753189 38.43050
## 71  28.1695092  15.9371201 40.40190
## 72  25.1673533  12.9414781 37.39323
## 73  29.3095684  17.0734684 41.54567
## 74  27.3904688  15.1602239 39.62071
## 75  28.1125063  15.8802848 40.34473
## 76  26.0603997  13.8330806 38.28772
## 77  23.1817501  10.9575858 35.40591
## 78  24.7968340  12.5714351 37.02223
## 79  22.8302319  10.6061548 35.05431
## 80  25.9083918  13.6813480 38.13544
## 81  29.5280798  17.2911908 41.76497
## 82  27.6944845  15.4634407 39.92553
## 83  28.1695092  15.9371201 40.40190
## 84  27.4189702  15.1886525 39.64929
## 85  25.4143661  13.1881334 37.64060
## 86  28.3500186  16.1170873 40.58295
## 87  22.3362062  10.1121422 34.56027
## 88  26.5354243  14.3071668 38.76368
## 89  29.3285694  17.0924018 41.56474
## 90  29.1385596  16.9030596 41.37406
## 91  26.1839061  13.9563544 38.41146
## 92  26.7634362  14.5346862 38.99219
## 93  26.8014382  14.5726034 39.03027
## 94  28.6540344  16.4201513 40.88792
## 95  24.4928182  12.2677563 36.71788
## 96  28.2360127  16.0034258 40.46860
## 97  23.7802812  11.5558190 36.00474
## 98  30.5541331  18.3132055 42.79506
## 99  31.1621647  18.9185842 43.40575
## 100 28.6730354  16.4390912 40.90698
## 101 25.6043760  13.3778464 37.83091
## 102 27.2669623  15.0370282 39.49690
## 103 24.4548162  12.2297930 36.67984
## 104 21.7851776   9.5609770 34.00938
## 105 22.8397323  10.6156538 35.06381
## 106 18.9065280   6.6790237 31.13403
## 107 16.8259199   4.5933220 29.05852
## 108 21.1676455   8.9431026 33.39219
## 109 22.8967353  10.6726467 35.12082
## 110 19.7805734   7.5545319 32.00661
## 111 22.2031993   9.9791169 34.42728
## 112 24.9013394  12.6758135 37.12687
## 113 19.1535409   6.9264905 31.38059
## 114 18.3174974   6.0887813 30.54621
## 115 24.6258251  12.4006217 36.85103
## 116 19.5810631   7.3547229 31.80740
## 117 23.1152467  10.8911038 35.33939
## 118 24.7683325  12.5429672 36.99370
## 119 19.9515823   7.7257801 32.17738
## 120 21.6236692   9.3993984 33.84794
## 121 20.9016317   8.6768796 33.12638
## 122 20.9966366   8.7719635 33.22131
## 123 17.5194560   5.2888080 29.75010
## 124 10.4130868  -1.8494546 22.67563
## 125 17.8519732   5.6221708 30.08178
## 126 20.4836100   8.2584542 32.70877
## 127  8.6554955  -3.6189936 20.92998
## 128 18.2224925   5.9935639 30.45142
## 129 19.9325813   7.7067533 32.15841
## 130 17.1299357   4.8982236 29.36165
## 131 22.5832190  10.3591645 34.80727
## 132 22.9062358  10.6821454 35.13033
## 133 23.9892921  11.7646815 36.21390
## 134 20.2745991   8.0492071 32.49999
## 135 18.1084866   5.8792968 30.33768
## 136 18.4410038   6.2125569 30.66945
## 137 18.4980068   6.2696814 30.72633
## 138 20.6926208   8.4676783 32.91756
## 139 14.2987887   2.0569550 26.54062
## 140 17.0159298   4.7838912 29.24797
## 141 11.6006485  -0.6547291 23.85603
## 142  1.8626426 -10.4730277 14.19831
## 143  9.0735172  -3.1979849 21.34502
## 144  9.4535370  -2.8153285 21.72240
## 145  6.7268953  -5.5625465 19.01634
## 146  8.1424688  -4.1358098 20.42075
## 147 18.7355191   6.5076818 30.96336
## 148  6.4988835  -5.7924533 18.79022
## 149  7.6484432  -4.6336136 19.93050
## 150 14.1752822   1.9329116 26.41765
## 151 21.1581450   8.9335952 33.38269
## 152 21.9371855   9.7130385 34.16133
## 153 23.0392427  10.8151216 35.26336
## 154 19.5525616   7.3261771 31.77895
## 155 20.1890947   7.9635994 32.41459
## 156 20.2840996   8.0587188 32.50948
## 157 19.2200443   6.9931108 31.44698
## 158 30.1931143  17.9536705 42.43256
## 159 28.4450235  16.2118000 40.67825
## 160 27.5329762  15.3023628 39.76359
## 161 29.3285694  17.0924018 41.56474
## 162 32.9102555  20.6579725 45.16254
## 163 32.7297461  20.4784355 44.98106
## 164 31.3996770  19.1550077 43.64435
## 165 23.4952664  11.2709695 35.71956
## 166 25.2338567  13.0078884 37.45983
## 167 31.0386583  18.7956323 43.28168
## 168 23.0202417  10.7961255 35.24436
## 169 24.0082931  11.7836679 36.23292
## 170 23.7992822  11.5748074 36.02376
## 171 20.8446287   8.6198270 33.06943
## 172 23.1247472  10.9006014 35.34889
## 173 20.5976159   8.3725793 32.82265
## 174 25.9653947  13.7382491 38.19254
## 175 25.3953651  13.1691610 37.62157
## 176 29.4900778  17.2533279 41.72683
## 177 24.9488419  12.7232563 37.17443
## 178 28.5780304  16.3443898 40.81167
## 179 27.9794994  15.7476625 40.21134
## 180 29.7655921  17.5278173 42.00337
## 181 27.3714678  15.1412712 39.60166
## 182 25.5758745  13.3493907 37.80236
## 183 29.9746030  17.7360242 42.21318
## 184 29.1575606  16.9219946 41.39313
## 185 21.2721509   9.0476800 33.49662
## 186 22.0606919   9.8365795 34.28480
## 187 30.3261213  18.0861387 42.56610
## 188 28.2075112  15.9750093 40.44001
## 189 30.2216158  17.9820574 42.46117
## 190 29.4330749  17.1965320 41.66962
## 191 29.7085892  17.4710296 41.94615
## 192 30.0981094  17.8590447 42.33717
## 193 31.8271992  19.5804959 44.07390
## 194 29.7750926  17.5372817 42.01290
## 195 30.3926247  18.1523693 42.63288
## 196 31.7321943  19.4859512 43.97844
## 197 30.6776395  18.4361887 42.91909
## 198 26.3739159  14.1459908 38.60184
## 199 28.2645142  16.0318418 40.49719
## 200 30.2216158  17.9820574 42.46117
## 201 30.3261213  18.0861387 42.56610
## 202 27.4949742  15.2644601 39.72549
## 203 31.5991874  19.3535807 43.84479
## 204 30.9341528  18.6915898 43.17672
## 205 31.8176987  19.5710417 44.06436
## 206 24.2268044  12.0019975 36.45161
## 207 24.1317995  11.9070747 36.35652
## 208 17.3959496   5.1649728 29.62693
## 209 20.6261174   8.4011095 32.85113
## 210 12.6172013   0.3673724 24.86703
## 211 18.1464885   5.9173866 30.37559
## 212 11.7716574  -0.4827491 24.02606
## 213 19.3245497   7.0977950 31.55130
## 214 25.6423779  13.4157868 37.86897
## 215  6.4798825  -5.8116134 18.77138
## 216 25.5568735  13.3304200 37.78333
## 217 21.7186741   9.4944463 33.94290
## 218 25.3478626  13.1217293 37.57400
## 219 17.5289565   5.2983335 29.75958
## 220 24.5783227  12.3531708 36.80347
## 221 25.3288617  13.1027563 37.55497
## 222 14.1657817   1.9233695 26.40819
## 223 25.1198508  12.8940407 37.34566
## 224 27.3334658  15.1033653 39.56357
## 225 30.6206366  18.3794282 42.86184
## 226 30.1551124  17.9158208 42.39440
## 227 31.5801864  19.3346699 43.82570
## 228 28.5115270  16.2780961 40.74496
## 229 30.8296474  18.5875417 43.07175
## 230 30.9816553  18.7388825 43.22443
## 231 23.4857659  11.2614738 35.71006
## 232 29.5660818  17.3290531 41.80311
## 233 32.2072190  19.9586275 44.45581
## 234 30.8011459  18.5591639 43.04313
## 235 26.9059436  14.6768719 39.13502
## 236 24.2173039  11.9925054 36.44210
## 237 25.4903700  13.2640209 37.71672
## 238 30.0601074  17.8211931 42.29902
## 239 28.5115270  16.2780961 40.74496
## 240 27.5519771  15.3213139 39.78264
## 241 23.7422792  11.5178415 35.96672
## 242 22.7732289  10.5491599 34.99730
## 243 23.8942871  11.6697469 36.11883
## 244 29.6230847  17.3858450 41.86032
## 245 22.6782240  10.4541646 34.90228
## 246 17.0159298   4.7838912 29.24797
## 247 25.8513888  13.6244451 38.07833
## 248 24.9108399  12.6853021 37.13638
## 249 25.5093710  13.2829923 37.73575
## 250 28.3215171  16.0886725 40.55436
## 251 28.9485497  16.7136984 41.18340
## 252 31.1431637  18.8996690 43.38666
## 253 31.2001667  18.9564139 43.44392
## 254 31.1906662  18.9469565 43.43438
## 255 28.3120166  16.0792009 40.54483
## 256 25.7658844  13.5390877 37.99268
## 257 31.5991874  19.3535807 43.84479
## 258 29.6895882  17.4521000 41.92708
## 259 27.1529564  14.9233020 39.38261
## 260 27.9985003  15.7666091 40.23039
## 261 25.4428676  13.2165915 37.66914
## 262 27.6564826  15.4255413 39.88742
## 263 28.9390492  16.7042299 41.17387
## 264 23.8657856  11.6412655 36.09031
## 265 26.8584411  14.6294778 39.08740
## 266 24.6258251  12.4006217 36.85103
## 267 20.5026109   8.2774755 32.72775
## 268 27.4854737  15.2549843 39.71596
## 269 31.5516849  19.3063033 43.79707
## 270 21.5856672   9.3613779 33.80996
## 271 22.2031993   9.9791169 34.42728
## 272 28.2930156  16.0602574 40.52577
## 273 27.2099594  14.9801660 39.43975
## 274 28.3025161  16.0697291 40.53530
## 275 31.2001667  18.9564139 43.44392
## 276 31.7226938  19.4764965 43.96889
## 277 28.8060423  16.5716652 41.04042
## 278 30.6016356  18.3605077 42.84276
## 279 27.7229860  15.4918648 39.95411
## 280 29.9461015  17.7076337 42.18457
## 281 30.9816553  18.7388825 43.22443
## 282 30.1931143  17.9536705 42.43256
## 283 31.6941923  19.4481320 43.94025
## 284 31.5516849  19.3063033 43.79707
## 285 27.0959535  14.8664364 39.32547
## 286 26.7349347  14.5062477 38.96362
## 287 22.2697027  10.0456307 34.49377
## 288 27.7704885  15.5392374 40.00174
## 289 27.3334658  15.1033653 39.56357
## 290 25.5188715  13.2924779 37.74527
## 291 31.3901765  19.1455513 43.63480
## 292 31.1716652  18.9280417 43.41529
## 293 30.0886089  17.8495819 42.32764
## 294 26.4024174  14.1744346 38.63040
## 295 24.6733276  12.4480714 36.89858
## 296 28.5970314  16.3633305 40.83073
## 297 27.5329762  15.3023628 39.76359
## 298 19.5050591   7.2785997 31.73152
## 299 29.8320956  17.5940674 42.07012
## 300 30.0506069  17.8117301 42.28948
## 301 28.7870413  16.5527266 41.02136
## 302 25.5283720  13.3019635 37.75478
## 303 26.3169130  14.0891018 38.54472
## 304 29.9366010  17.6981701 42.17503
## 305 27.9699989  15.7381892 40.20181
## 306 26.0699002  13.8425634 38.29724
## 307 28.4070216  16.1739155 40.64013
## 308 27.3999692  15.1697001 39.63024
## 309 30.2406168  18.0009817 42.48025
## 310 25.0818488  12.8560899 37.30761
## 311 22.5452170  10.3211632 34.76927
## 312 28.8725457  16.6379487 41.10714
## 313 23.4192625  11.1950024 35.64352
## 314 27.0484510  14.8190470 39.27785
## 315 25.7373829  13.5106343 37.96413
## 316 23.6282733  11.4039045 35.85264
## 317 17.1394362   4.9077510 29.37112
## 318 19.4100542   7.1834415 31.63667
## 319 24.7113296  12.4860303 36.93663
## 320 22.4597126  10.2356574 34.68377
## 321 27.7134855  15.4823902 39.94458
## 322 28.0270018  15.7950287 40.25897
## 323 27.2384609  15.0085973 39.46832
## 324 23.4002615  11.1760102 35.62451
## 325 28.7395388  16.5053793 40.97370
## 326 29.7275902  17.4899590 41.96522
## 327 28.7110374  16.4769704 40.94510
## 328 22.4027096  10.1786514 34.62677
## 329 25.0818488  12.8560899 37.30761
## 330 27.5804786  15.3497401 39.81122
## 331 25.9178923  13.6908316 38.14495
## 332 22.7447274  10.5206618 34.96879
## 333 27.1149544  14.8853918 39.34452
## 334 29.1575606  16.9219946 41.39313
## 335 28.1410077  15.9087027 40.37331
## 336 26.9439456  14.7147863 39.17310
## 337 25.2433572  13.0173754 37.46934
## 338 24.5213197  12.2962282 36.74641
## 339 26.4689209  14.2408018 38.69704
## 340 25.3003602  13.0742964 37.52642
## 341 25.7278824  13.5011498 37.95461
## 342 29.3380699  17.1018684 41.57427
## 343 26.3359140  14.1080650 38.56376
## 344 27.7324865  15.5013394 39.96363
## 345 30.1741134  17.9347458 42.41348
## 346 24.5498212  12.3246997 36.77494
## 347 22.5167156  10.2926617 34.74077
## 348 28.5115270  16.2780961 40.74496
## 349 28.8630453  16.6284797 41.09761
## 350 28.9580502  16.7231669 41.19293
## 351 28.8725457  16.6379487 41.10714
## 352 29.3380699  17.1018684 41.57427
## 353 27.1529564  14.9233020 39.38261
## 354 30.2786188  18.0388297 42.51841
## 355 26.9059436  14.6768719 39.13502
## 356 29.2620660  17.0261342 41.49800
## 357 17.8329723   5.6031231 30.06282
## 358 21.9466860   9.7225420 34.17083
## 359 23.6472743  11.4228945 35.87165
## 360 22.5167156  10.2926617 34.74077
## 361 27.1529564  14.9233020 39.38261
## 362 21.0726405   8.8480272 33.29725
## 363 24.8728380  12.6473472 37.09833
## 364 20.6451183   8.4201294 32.87011
## 365 29.5280798  17.2911908 41.76497
## 366 27.7894895  15.5581861 40.02079
## 367 21.2531499   9.0286664 33.47763
## 368 21.8896830   9.6655206 34.11385
## 369 31.4566800  19.2117450 43.70161
## 370 31.0101568  18.7672576 43.25306
## 371 31.7416948  19.4954059 43.98798
## 372 25.4998705  13.2735066 37.72623
## 373 26.1174026  13.8899772 38.34483
## 374  1.5206248 -10.8187523 13.86000
## 375 -1.5195331 -13.8944771 10.85541
## 376 21.7851776   9.5609770 34.00938
## 377 12.4746939   0.2241196 24.72527
## 378 14.3747926   2.1332855 26.61630
## 379 12.0471717  -0.2057023 24.30005
## 380 13.8617660   1.6179965 26.10554
## 381 18.2034915   5.9745199 30.43246
## 382 14.5268005   2.2859373 26.76766
## 383 12.1326761  -0.1197303 24.38508
## 384 11.2206288  -1.0369614 23.47822
## 385  5.4538292  -6.8465360 17.75419
## 386  5.2828203  -7.0190758 17.58472
## 387  7.6864452  -4.5953165 19.96821
## 388  4.1617621  -8.1505431 16.47407
## 389  5.4633297  -6.8369508 17.76361
## 390 14.7453119   2.5053531 26.98527
## 391 18.2984964   6.0697382 30.52725
## 392 16.7309150   4.4980303 28.96380
## 393 10.1565735  -2.1076117 22.42076
## 394 20.1415922   7.9160379 32.36715
## 395 19.0205339   6.7932431 31.24782
## 396 18.2889959   6.0602166 30.51778
## 397 16.1513849   3.9166488 28.38612
## 398 15.6288578   3.3923016 27.86541
## 399  5.4918311  -6.8081958 17.79186
## 400  6.0808617  -6.2140187 18.37574
## 401  9.1210197  -3.1501488 21.39219
## 402 15.2488380   3.0108684 27.48681
## 403 15.2583385   3.0204051 27.49627
## 404 15.7713652   3.5353195 28.00741
## 405  8.5414896  -3.7338299 20.81681
## 406 12.7217067   0.4724177 24.97100
## 407 12.3796890   0.1286119 24.63077
## 408 23.0297422  10.8056236 35.25386
## 409  9.4725379  -2.7961976 21.74127
## 410 15.7618647   3.5257853 27.99794
## 411 24.9488419  12.7232563 37.17443
## 412 14.3937936   2.1523676 26.63522
## 413  1.9006446 -10.4346175 14.23591
## 414 15.4768499   3.2397374 27.71396
## 415 -0.5789842 -12.9424233 11.78445
## 416  6.9549072  -5.3326665 19.24248
## 417 10.0520680  -2.2127966 22.31693
## 418  9.2445261  -3.0257802 21.51483
## 419 14.9638232   2.7247439 27.20290
## 420 12.9497186   0.7015878 25.19785
## 421 20.2840996   8.0587188 32.50948
## 422 19.6380660   7.4118133 31.86432
## 423 21.1581450   8.9335952 33.38269
## 424 12.4271914   0.1763663 24.67802
## 425 18.2509940   6.0221297 30.47986
## 426 11.3821371  -0.8745035 23.63878
## 427 19.6475665   7.4213282 31.87380
## 428 20.7591243   8.5342449 32.98400
## 429 14.1087788   1.8661158 26.35144
## 430 11.6766524  -0.5782917 23.93160
## 431 17.7949703   5.5650270 30.02491
## 432 15.8473691   3.6115914 28.08315
## 433 23.1247472  10.9006014 35.34889
## 434 19.1440404   6.9169732 31.37111
## 435 20.1415922   7.9160379 32.36715
## 436 12.4461924   0.1954678 24.69692
## 437 17.4054500   5.1744988 29.63640
## 438  9.4250355  -2.8440251 21.69410
## 439  2.2331619 -10.0985601 14.56488
## 440 12.8167117   0.5679085 25.06551
## 441 13.5482497   1.3030300 25.79347
## 442 16.0088775   3.7736592 28.24410
## 443 18.7925221   6.5647974 31.02025
## 444 16.6454106   4.4122638 28.87856
## 445 11.9521668  -0.3012312 24.20556
## 446 11.7716574  -0.4827491 24.02606
## 447 17.6524629   5.4221601 29.88277
## 448 18.9350295   6.7075792 31.16248
## 449 17.3294461   5.0982890 29.56060
## 450 16.2083879   3.9738417 28.44293
## 451 17.9849801   5.7554997 30.21446
## 452 17.7094658   5.4793081 29.93962
## 453 18.1464885   5.9173866 30.37559
## 454 18.6500147   6.4220050 30.87802
## 455 16.7784175   4.5456768 29.01116
## 456 17.3294461   5.0982890 29.56060
## 457 16.4934027   4.2597805 28.72702
## 458 18.4600048   6.2315986 30.68841
## 459 19.1345399   6.9074558 31.36162
## 460 20.5881154   8.3630691 32.81316
## 461 18.9540305   6.7266160 31.18145
## 462 20.6356178   8.4106195 32.86062
## 463 21.2626504   9.0381732 33.48713
## 464 24.7778330  12.5524566 37.00321
## 465 21.9941884   9.7700585 34.21832
## 466 21.1296435   8.9050730 33.35421
## 467 18.2604945   6.0316515 30.48934
## 468 14.2987887   2.0569550 26.54062
## 469 17.3294461   5.0982890 29.56060
## 470 20.5311124   8.3060071 32.75622
## 471 19.0775369   6.8503503 31.30472
## 472 22.3267057  10.1026407 34.55077
## 473 20.9111322   8.6863882 33.13588
## 474 23.4762654  11.2519780 35.70055
## 475 17.3199456   5.0887625 29.55113
## 476 11.6576515  -0.5974008 23.91270
## 477 16.8069190   4.5742641 29.03957
## 478 10.8881115  -1.3714764 23.14770
## 479 17.4244510   5.1935508 29.65535
## 480 22.0986939   9.8745906 34.32280
## 481 24.3503108  12.1253901 36.57523
## 482 27.2004589  14.9706888 39.43023
## 483 27.8939949  15.6624005 40.12559
## 484 24.6543266  12.4290917 36.87956
## 485 21.8801825   9.6560169 34.10435
## 486 24.5023187  12.2772470 36.72739
## 487 20.3221016   8.0967652 32.54744
## 488 23.6757758  11.4513791 35.90017
## 489 17.3959496   5.1649728 29.62693
## 490 11.7811579  -0.4731951 24.03551
## 491  6.3563761  -5.9361586 18.64891
## 492 17.3864491   5.1554467 29.61745
## 493 21.8706820   9.6465131 34.09485
## 494 23.1437481  10.9195964 35.36790
## 495 21.6426702   9.4184084 33.86693
## 496 17.8329723   5.6031231 30.06282
## 497 14.4697975   2.2286943 26.71090
## 498 21.1581450   8.9335952 33.38269
## 499 22.2792032  10.0551325 34.50327
## 500 20.2080956   7.9826236 32.43357
## 501 20.9396336   8.7149137 33.16435
## 502 25.3668636  13.1407021 37.59303
## 503 25.9273927  13.7003152 38.15447
## 504 29.1955625  16.9598642 41.43126
## 505 28.3975211  16.1644442 40.63060
## 506 27.0674520  14.8380029 39.29690
plot(Boston$lstat,Boston$medv)
abline(lm.fit,col="red",lwd=3)

par(mfrow=c(2,2))
plot(lm.fit)

plot(hatvalues(lm.fit))
which.max(hatvalues(lm.fit))
## 375 
## 375
# 3.6.3 Multiple Linear Regression
lm.fit1 <- lm(Boston$medv~Boston$lstat+Boston$age,
              data = Boston)
lm.fit1
## 
## Call:
## lm(formula = Boston$medv ~ Boston$lstat + Boston$age, data = Boston)
## 
## Coefficients:
##  (Intercept)  Boston$lstat    Boston$age  
##     33.22276      -1.03207       0.03454
summary(lm.fit1)
## 
## Call:
## lm(formula = Boston$medv ~ Boston$lstat + Boston$age, data = Boston)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -15.981  -3.978  -1.283   1.968  23.158 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  33.22276    0.73085  45.458  < 2e-16 ***
## Boston$lstat -1.03207    0.04819 -21.416  < 2e-16 ***
## Boston$age    0.03454    0.01223   2.826  0.00491 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.173 on 503 degrees of freedom
## Multiple R-squared:  0.5513, Adjusted R-squared:  0.5495 
## F-statistic:   309 on 2 and 503 DF,  p-value: < 2.2e-16
lm.fit2 <- lm(Boston$medv~.,data = Boston)
lm.fit2
## 
## Call:
## lm(formula = Boston$medv ~ ., data = Boston)
## 
## Coefficients:
## (Intercept)         crim           zn        indus         chas  
##   3.646e+01   -1.080e-01    4.642e-02    2.056e-02    2.687e+00  
##         nox           rm          age          dis          rad  
##  -1.777e+01    3.810e+00    6.922e-04   -1.476e+00    3.060e-01  
##         tax      ptratio        black        lstat  
##  -1.233e-02   -9.527e-01    9.312e-03   -5.248e-01
summary(lm.fit2)
## 
## Call:
## lm(formula = Boston$medv ~ ., data = Boston)
## 
## 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    
## chas         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 ***
## black        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
lm.fit3 <-update(lm.fit2,~.-age)
summary(lm.fit3)
## 
## Call:
## lm(formula = Boston$medv ~ crim + zn + indus + chas + nox + rm + 
##     dis + rad + tax + ptratio + black + lstat, data = Boston)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15.6054  -2.7313  -0.5188   1.7601  26.2243 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  36.436927   5.080119   7.172 2.72e-12 ***
## crim         -0.108006   0.032832  -3.290 0.001075 ** 
## zn            0.046334   0.013613   3.404 0.000719 ***
## indus         0.020562   0.061433   0.335 0.737989    
## chas          2.689026   0.859598   3.128 0.001863 ** 
## nox         -17.713540   3.679308  -4.814 1.97e-06 ***
## rm            3.814394   0.408480   9.338  < 2e-16 ***
## dis          -1.478612   0.190611  -7.757 5.03e-14 ***
## rad           0.305786   0.066089   4.627 4.75e-06 ***
## tax          -0.012329   0.003755  -3.283 0.001099 ** 
## ptratio      -0.952211   0.130294  -7.308 1.10e-12 ***
## black         0.009321   0.002678   3.481 0.000544 ***
## lstat        -0.523852   0.047625 -10.999  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.74 on 493 degrees of freedom
## Multiple R-squared:  0.7406, Adjusted R-squared:  0.7343 
## F-statistic: 117.3 on 12 and 493 DF,  p-value: < 2.2e-16
# 3.6.4 Interaction Terms
summary(lm(Boston$medv~Boston$lstat*Boston$age,
           data = Boston))
## 
## Call:
## lm(formula = Boston$medv ~ Boston$lstat * Boston$age, data = Boston)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -15.806  -4.045  -1.333   2.085  27.552 
## 
## Coefficients:
##                           Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             36.0885359  1.4698355  24.553  < 2e-16 ***
## Boston$lstat            -1.3921168  0.1674555  -8.313 8.78e-16 ***
## Boston$age              -0.0007209  0.0198792  -0.036   0.9711    
## Boston$lstat:Boston$age  0.0041560  0.0018518   2.244   0.0252 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.149 on 502 degrees of freedom
## Multiple R-squared:  0.5557, Adjusted R-squared:  0.5531 
## F-statistic: 209.3 on 3 and 502 DF,  p-value: < 2.2e-16
# 3.6.5 Non-linear Transformations 
# of the Predictors
lm.fit4 <- lm(Boston$medv~Boston$lstat+I(Boston$lstat^2))
summary(lm.fit4)
## 
## Call:
## lm(formula = Boston$medv ~ Boston$lstat + I(Boston$lstat^2))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15.2834  -3.8313  -0.5295   2.3095  25.4148 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       42.862007   0.872084   49.15   <2e-16 ***
## Boston$lstat      -2.332821   0.123803  -18.84   <2e-16 ***
## I(Boston$lstat^2)  0.043547   0.003745   11.63   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.524 on 503 degrees of freedom
## Multiple R-squared:  0.6407, Adjusted R-squared:  0.6393 
## F-statistic: 448.5 on 2 and 503 DF,  p-value: < 2.2e-16
anova(lm.fit,lm.fit4)
## Analysis of Variance Table
## 
## Model 1: Boston$medv ~ Boston$lstat
## Model 2: Boston$medv ~ Boston$lstat + I(Boston$lstat^2)
##   Res.Df   RSS Df Sum of Sq     F    Pr(>F)    
## 1    504 19472                                 
## 2    503 15347  1    4125.1 135.2 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
par(mfrow=c(2,2))

plot(lm.fit4)

# using the poly() function
# poly()
# to create the polynomial within lm()
lm.fit5 <- lm(Boston$medv~poly(Boston$lstat,5))
summary(lm.fit5)
## 
## Call:
## lm(formula = Boston$medv ~ poly(Boston$lstat, 5))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.5433  -3.1039  -0.7052   2.0844  27.1153 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)              22.5328     0.2318  97.197  < 2e-16 ***
## poly(Boston$lstat, 5)1 -152.4595     5.2148 -29.236  < 2e-16 ***
## poly(Boston$lstat, 5)2   64.2272     5.2148  12.316  < 2e-16 ***
## poly(Boston$lstat, 5)3  -27.0511     5.2148  -5.187 3.10e-07 ***
## poly(Boston$lstat, 5)4   25.4517     5.2148   4.881 1.42e-06 ***
## poly(Boston$lstat, 5)5  -19.2524     5.2148  -3.692 0.000247 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.215 on 500 degrees of freedom
## Multiple R-squared:  0.6817, Adjusted R-squared:  0.6785 
## F-statistic: 214.2 on 5 and 500 DF,  p-value: < 2.2e-16
# Of course, we are in no way 
# restricted to using polynomial transformations
# of the predictors. Here we try a 
# log transformation
summary(lm(Boston$medv~log(Boston$rm),data = Boston))
## 
## Call:
## lm(formula = Boston$medv ~ log(Boston$rm), data = Boston)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -19.487  -2.875  -0.104   2.837  39.816 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -76.488      5.028  -15.21   <2e-16 ***
## log(Boston$rm)   54.055      2.739   19.73   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.915 on 504 degrees of freedom
## Multiple R-squared:  0.4358, Adjusted R-squared:  0.4347 
## F-statistic: 389.3 on 1 and 504 DF,  p-value: < 2.2e-16
# 3.6.6 Qualitative Predictors
library(ISLR)
data(Carseats)
# The Carseats data includes qualitative predictors
# such as Shelveloc, an indicator
# of the quality of the shelving location-that is,
# the space within
# a store in which the car seat is displayed-at
# each location. The predictor
# Shelveloc takes on three possible values, Bad,
# Medium, and Good.
# Given a qualitative variable such as
# Shelveloc, R generates dummy variables
# automatically
names(Carseats)
##  [1] "Sales"       "CompPrice"   "Income"      "Advertising" "Population" 
##  [6] "Price"       "ShelveLoc"   "Age"         "Education"   "Urban"      
## [11] "US"
str(Carseats)
## 'data.frame':    400 obs. of  11 variables:
##  $ Sales      : num  9.5 11.22 10.06 7.4 4.15 ...
##  $ CompPrice  : num  138 111 113 117 141 124 115 136 132 132 ...
##  $ Income     : num  73 48 35 100 64 113 105 81 110 113 ...
##  $ Advertising: num  11 16 10 4 3 13 0 15 0 0 ...
##  $ Population : num  276 260 269 466 340 501 45 425 108 131 ...
##  $ Price      : num  120 83 80 97 128 72 108 120 124 124 ...
##  $ ShelveLoc  : Factor w/ 3 levels "Bad","Good","Medium": 1 2 3 3 1 1 3 2 3 3 ...
##  $ Age        : num  42 65 59 55 38 78 71 67 76 76 ...
##  $ Education  : num  17 10 12 14 13 16 15 10 10 17 ...
##  $ Urban      : Factor w/ 2 levels "No","Yes": 2 2 2 2 2 1 2 2 1 1 ...
##  $ US         : Factor w/ 2 levels "No","Yes": 2 2 2 2 1 2 1 2 1 2 ...
lm.fit_1 <- lm(Sales~.+Income:Advertising+Price:Age,
               data = Carseats)
summary(lm.fit_1)
## 
## Call:
## lm(formula = Sales ~ . + Income:Advertising + Price:Age, data = Carseats)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.9208 -0.7503  0.0177  0.6754  3.3413 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         6.5755654  1.0087470   6.519 2.22e-10 ***
## CompPrice           0.0929371  0.0041183  22.567  < 2e-16 ***
## Income              0.0108940  0.0026044   4.183 3.57e-05 ***
## Advertising         0.0702462  0.0226091   3.107 0.002030 ** 
## Population          0.0001592  0.0003679   0.433 0.665330    
## Price              -0.1008064  0.0074399 -13.549  < 2e-16 ***
## ShelveLocGood       4.8486762  0.1528378  31.724  < 2e-16 ***
## ShelveLocMedium     1.9532620  0.1257682  15.531  < 2e-16 ***
## Age                -0.0579466  0.0159506  -3.633 0.000318 ***
## Education          -0.0208525  0.0196131  -1.063 0.288361    
## UrbanYes            0.1401597  0.1124019   1.247 0.213171    
## USYes              -0.1575571  0.1489234  -1.058 0.290729    
## Income:Advertising  0.0007510  0.0002784   2.698 0.007290 ** 
## Price:Age           0.0001068  0.0001333   0.801 0.423812    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.011 on 386 degrees of freedom
## Multiple R-squared:  0.8761, Adjusted R-squared:  0.8719 
## F-statistic:   210 on 13 and 386 DF,  p-value: < 2.2e-16
contrasts(Carseats$ShelveLoc)
##        Good Medium
## Bad       0      0
## Good      1      0
## Medium    0      1