crime <- read.csv("~/Downloads/crime.csv")
str(crime)
## 'data.frame':    50 obs. of  9 variables:
##  $ state        : chr  "Alabama" "Alaska" "Arizona" "Arkansas" ...
##  $ murder.rate  : num  7.4 4.3 7 6.3 6.1 3.1 2.9 3.2 5.6 8 ...
##  $ poverty      : num  14.7 8.4 13.5 15.8 14 8.5 7.7 9.9 12 12.5 ...
##  $ high.school  : num  77.5 90.4 85.1 81.7 81.2 89.7 88.2 86.1 84 82.6 ...
##  $ college      : num  20.4 28.1 24.6 18.4 27.5 34.6 31.6 24 22.8 23.1 ...
##  $ single.parent: num  26 23.2 23.5 24.7 21.8 20.8 22.9 25.6 26.5 25.5 ...
##  $ unemployed   : num  4.6 6.6 3.9 4.4 4.9 2.7 2.3 4 3.6 3.7 ...
##  $ metropolitan : num  70.2 41.6 87.9 49 96.7 84 95.6 81.4 93 69.1 ...
##  $ region       : chr  "South" "West" "West" "South" ...
crime.data = crime[,-1]
crime[,-1]
##    murder.rate poverty high.school college single.parent unemployed
## 1          7.4    14.7        77.5    20.4          26.0        4.6
## 2          4.3     8.4        90.4    28.1          23.2        6.6
## 3          7.0    13.5        85.1    24.6          23.5        3.9
## 4          6.3    15.8        81.7    18.4          24.7        4.4
## 5          6.1    14.0        81.2    27.5          21.8        4.9
## 6          3.1     8.5        89.7    34.6          20.8        2.7
## 7          2.9     7.7        88.2    31.6          22.9        2.3
## 8          3.2     9.9        86.1    24.0          25.6        4.0
## 9          5.6    12.0        84.0    22.8          26.5        3.6
## 10         8.0    12.5        82.6    23.1          25.5        3.7
## 11         2.9    10.6        87.4    26.3          19.1        4.3
## 12         1.2    13.3        86.2    20.0          17.7        4.9
## 13         7.2    10.5        85.5    27.1          21.9        4.4
## 14         5.8     8.3        84.6    17.1          22.8        3.2
## 15         1.6     7.9        89.7    25.5          19.8        2.6
## 16         6.3    10.5        88.1    27.3          20.2        3.7
## 17         4.8    12.5        78.7    20.5          23.2        4.1
## 18        12.5    18.5        80.8    22.5          29.3        5.5
## 19         1.2     9.8        89.3    24.1          23.7        3.5
## 20         8.1     7.3        85.7    32.3          24.5        3.9
## 21         2.0    10.2        85.1    32.7          22.8        2.6
## 22         6.7    10.2        86.2    23.0          24.5        3.6
## 23         3.1     7.9        90.8    31.2          19.6        3.3
## 24         9.0    15.5        80.3    18.7          30.0        5.7
## 25         6.2     9.8        86.6    26.2          24.3        3.5
## 26         1.8    16.0        89.6    23.8          21.4        4.9
## 27         3.7    10.7        90.4    24.6          19.6        3.0
## 28         6.5    10.1        82.8    19.3          24.2        4.1
## 29         1.8     7.6        88.1    30.1          20.0        2.8
## 30         3.4     8.1        87.3    30.1          20.2        3.8
## 31         7.4    19.3        82.2    23.6          26.6        4.9
## 32         5.0    14.7        82.5    28.7          26.0        4.6
## 33         7.0    13.2        79.2    23.2          24.3        3.6
## 34         0.6    12.8        85.5    22.6          19.1        3.0
## 35         3.7    11.1        87.0    24.6          24.6        4.1
## 36         5.3    14.1        86.1    22.5          23.5        3.0
## 37         2.0    12.9        88.1    27.2          22.5        4.9
## 38         4.9     9.8        85.7    24.3          22.8        4.2
## 39         4.3    10.2        81.3    26.4          27.4        4.1
## 40         5.8    12.0        83.0    19.0          27.1        3.9
## 41         0.9     9.4        91.8    25.7          20.7        2.3
## 42         7.2    13.4        79.9    22.0          27.9        3.9
## 43         5.9    14.9        79.2    23.9          21.5        4.2
## 44         1.9     8.1        90.7    26.4          13.6        3.2
## 45         1.5    10.3        90.0    28.8          22.5        2.9
## 46         5.7     8.1        86.6    31.9          22.2        2.2
## 47         3.3     9.5        91.8    28.6          22.1        5.2
## 48         2.5    15.8        77.1    15.3          22.3        5.5
## 49         3.2     9.0        86.7    23.8          21.7        3.5
## 50         2.4    11.1        90.0    20.6          20.8        3.9
##    metropolitan        region
## 1          70.2         South
## 2          41.6          West
## 3          87.9          West
## 4          49.0         South
## 5          96.7          West
## 6          84.0          West
## 7          95.6     Northeast
## 8          81.4         South
## 9          93.0         South
## 10         69.1         South
## 11         72.9          West
## 12         38.6          West
## 13         84.5 North Central
## 14         71.8 North Central
## 15         44.9 North Central
## 16         56.8 North Central
## 17         48.4         South
## 18         75.2         South
## 19         36.3     Northeast
## 20         92.7         South
## 21         92.1     Northeast
## 22         82.5 North Central
## 23         70.3 North Central
## 24         36.2         South
## 25         68.0 North Central
## 26         33.4          West
## 27         52.2 North Central
## 28         86.6          West
## 29         60.3     Northeast
## 30        100.0     Northeast
## 31         57.0          West
## 32         91.9     Northeast
## 33         67.2         South
## 34         43.4 North Central
## 35         80.9 North Central
## 36         60.6         South
## 37         72.8          West
## 38         84.5     Northeast
## 39         93.8     Northeast
## 40         70.2         South
## 41         34.5 North Central
## 42         67.9         South
## 43         84.6         South
## 44         76.4          West
## 45         27.9     Northeast
## 46         78.2         South
## 47         83.0          West
## 48         41.9         South
## 49         67.8 North Central
## 50         29.6          West
cor(crime.data[,-8])
##               murder.rate    poverty high.school    college single.parent
## murder.rate     1.0000000  0.4267281  -0.6022432 -0.2368626     0.6773191
## poverty         0.4267281  1.0000000  -0.6348570 -0.5418042     0.4531953
## high.school    -0.6022432 -0.6348570   1.0000000  0.5273218    -0.5902236
## college        -0.2368626 -0.5418042   0.5273218  1.0000000    -0.2930359
## single.parent   0.6773191  0.4531953  -0.5902236 -0.2930359     1.0000000
## unemployed      0.3511507  0.5509692  -0.3662625 -0.3664921     0.3488836
## metropolitan    0.3266999 -0.2104392  -0.1808496  0.4056572     0.1622998
##                unemployed metropolitan
## murder.rate    0.35115068   0.32669995
## poverty        0.55096924  -0.21043915
## high.school   -0.36626246  -0.18084959
## college       -0.36649208   0.40565723
## single.parent  0.34888360   0.16229981
## unemployed     1.00000000  -0.09394706
## metropolitan  -0.09394706   1.00000000
sr=
model.matrix.lm(murder.rate~single.parent,data=crime.data)
summary(sr)
##   (Intercept) single.parent  
##  Min.   :1    Min.   :13.60  
##  1st Qu.:1    1st Qu.:20.95  
##  Median :1    Median :22.80  
##  Mean   :1    Mean   :22.97  
##  3rd Qu.:1    3rd Qu.:24.57  
##  Max.   :1    Max.   :30.00
# important variables are poverty,unemployed, college, metropolitan. (have a p-value below. 0.5)
model = lm(murder.rate ~ poverty + unemployed + college + metropolitan, data = crime.data)
summary(model)
## 
## Call:
## lm(formula = murder.rate ~ poverty + unemployed + college + metropolitan, 
##     data = crime.data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.5258 -1.4857 -0.0565  1.1470  4.4942 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -1.37245    3.10684  -0.442 0.660785    
## poverty       0.29931    0.13111   2.283 0.027206 *  
## unemployed    0.34868    0.36791   0.948 0.348331    
## college      -0.11589    0.08581  -1.351 0.183593    
## metropolitan  0.06025    0.01541   3.911 0.000307 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.022 on 45 degrees of freedom
## Multiple R-squared:  0.404,  Adjusted R-squared:  0.3511 
## F-statistic: 7.627 on 4 and 45 DF,  p-value: 8.835e-05