setwd("C:/Users/Taiyyab Ali/Desktop/R language")
 Housingpriceindex <- read.csv("Housingpriceindex.csv")
 View(Housingpriceindex)
cor(Housingpriceindex$index_nsa,Housingpriceindex$Population,method = "pearson")
## [1] 0.1021095

Positive correlation between population and housing price index, as expected with increase in population housing price index increases but slop of housing price index is very high in comparison to population, which can be the reason of weak correlation.

cor.test(Housingpriceindex$index_nsa,Housingpriceindex$Population,method = "pearson")
## 
##  Pearson's product-moment correlation
## 
## data:  Housingpriceindex$index_nsa and Housingpriceindex$Population
## t = 4.2397, df = 1706, p-value = 2.358e-05
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.05494486 0.14881948
## sample estimates:
##       cor 
## 0.1021095

As p-value is less than 0.01, this correlation is statistically significant.

cor.test(Housingpriceindex$index_nsa,Housingpriceindex$Violent.Crimes)
## 
##  Pearson's product-moment correlation
## 
## data:  Housingpriceindex$index_nsa and Housingpriceindex$Violent.Crimes
## t = -1.4656, df = 1706, p-value = 0.1429
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.08275273  0.01198973
## sample estimates:
##         cor 
## -0.03546118

People don’t like to buy houses in vicinity of area full of crime, so the correlation is negative.

cor.test(Housingpriceindex$index_nsa,Housingpriceindex$Homicides)
## 
##  Pearson's product-moment correlation
## 
## data:  Housingpriceindex$index_nsa and Housingpriceindex$Homicides
## t = -3.5359, df = 1706, p-value = 0.0004173
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.13219098 -0.03801809
## sample estimates:
##         cor 
## -0.08529503

Correlation is negative as expected and also statistically significant with p-value = 0.0004173.Usually people avoid these kind of area to live because negative things affect minds to do negative.

cor.test(Housingpriceindex$index_nsa,Housingpriceindex$Rapes)
## 
##  Pearson's product-moment correlation
## 
## data:  Housingpriceindex$index_nsa and Housingpriceindex$Rapes
## t = -4.885, df = 1706, p-value = 1.131e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.16396959 -0.07041383
## sample estimates:
##        cor 
## -0.1174523

Negative correlation with p-value = 1.131e-0.6. statistically significnt. Again people don’t like to buy house where rape cases are high, it’s not safe for there children.

cor.test(Housingpriceindex$index_nsa,Housingpriceindex$Assaults)
## 
##  Pearson's product-moment correlation
## 
## data:  Housingpriceindex$index_nsa and Housingpriceindex$Assaults
## t = 1.5716, df = 1706, p-value = 0.1162
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.009426004  0.085298648
## sample estimates:
##        cor 
## 0.03802174

Weak positive correlation with p-value = 0.1126, which is statistically insignificant.

cor.test(Housingpriceindex$index_nsa,Housingpriceindex$Robberies)
## 
##  Pearson's product-moment correlation
## 
## data:  Housingpriceindex$index_nsa and Housingpriceindex$Robberies
## t = -3.9478, df = 1706, p-value = 8.208e-05
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.14193599 -0.04793135
## sample estimates:
##         cor 
## -0.09514578

Weak negative correlation as expected with p-value 8.208e-05,which is statistically significant.

library(corrgram)
corrgram(Housingpriceindex[ ,c(1,2,4:9)],order = TRUE,upper.panel = panel.pie, lower.panel = panel.shade,text.panel = panel.txt, main= "corrgram plot of housing price index with each variable",diag.panel = panel.minmax)

Model1 = index_nsa ~ Year + Population + Homicides + Robberies + Rapes + Violent.Crimes + Assaults + City..State 
fit <- lm(Model1, data = Housingpriceindex)
summary(fit)
## 
## Call:
## lm(formula = Model1, data = Housingpriceindex)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -84.289 -15.412  -2.201  10.405 182.836 
## 
## Coefficients:
##                               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                 -9.360e+03  1.282e+02 -73.007  < 2e-16 ***
## Year                         4.748e+00  6.417e-02  74.003  < 2e-16 ***
## Population                   5.613e-06  1.576e-06   3.562 0.000378 ***
## Homicides                    4.162e-03  1.218e-02   0.342 0.732642    
## Robberies                    7.298e-04  5.229e-04   1.396 0.163040    
## Rapes                       -1.106e-02  3.995e-03  -2.769 0.005679 ** 
## Violent.Crimes              -9.682e-05  3.736e-04  -0.259 0.795527    
## Assaults                     7.458e-05  7.445e-04   0.100 0.920211    
## City..StateArlington, TX     1.725e+00  6.244e+00   0.276 0.782419    
## City..StateAtlanta, GA       1.800e+00  6.129e+00   0.294 0.768990    
## City..StateAurora, CO        3.150e+01  6.210e+00   5.073 4.37e-07 ***
## City..StateAustin, TX        2.932e+01  6.171e+00   4.751 2.20e-06 ***
## City..StateBaltimore, MD     9.210e+00  6.148e+00   1.498 0.134281    
## City..StateBoston, MA        2.259e+01  6.240e+00   3.620 0.000304 ***
## City..StateBuffalo, NY      -1.590e+01  6.187e+00  -2.570 0.010249 *  
## City..StateCharlotte, NC    -5.170e+00  6.234e+00  -0.829 0.407051    
## City..StateChicago, IL       1.124e+00  6.117e+00   0.184 0.854237    
## City..StateCincinnati, OH   -6.436e+00  6.158e+00  -1.045 0.296118    
## City..StateCleveland, OH    -9.708e+00  6.109e+00  -1.589 0.112198    
## City..StateColumbus, OH     -5.089e+00  6.153e+00  -0.827 0.408337    
## City..StateDallas, TX        5.267e+00  6.180e+00   0.852 0.394213    
## City..StateDenver, CO        3.208e+01  6.190e+00   5.183 2.46e-07 ***
## City..StateDetroit, MI      -4.220e+00  6.178e+00  -0.683 0.494706    
## City..StateFresno, CA        5.140e+00  6.225e+00   0.826 0.409060    
## City..StateHonolulu, HI     -1.526e+01  6.262e+00  -2.436 0.014952 *  
## City..StateHouston, TX       1.253e+01  6.199e+00   2.021 0.043476 *  
## City..StateIndianapolis, IN -9.631e+00  6.241e+00  -1.543 0.122967    
## City..StateJacksonville, FL  1.443e+01  6.267e+00   2.303 0.021392 *  
## City..StateLouisville, KY    1.080e+00  6.227e+00   0.173 0.862357    
## City..StateMemphis, TN      -4.843e+00  6.217e+00  -0.779 0.436099    
## City..StateMesa, AZ          2.342e+01  6.269e+00   3.736 0.000194 ***
## City..StateMiami, FL         3.096e+01  6.183e+00   5.007 6.10e-07 ***
## City..StateMilwaukee, WI     4.964e+00  6.331e+00   0.784 0.433169    
## City..StateMinneapolis, MN   1.904e+01  6.200e+00   3.072 0.002164 ** 
## City..StateNashville, TN     7.028e+00  6.311e+00   1.114 0.265562    
## City..StateNewark, NJ        1.271e+01  6.216e+00   2.044 0.041125 *  
## City..StateOakland, CA       1.399e+01  6.223e+00   2.249 0.024664 *  
## City..StateOmaha, NE         4.025e+00  6.276e+00   0.641 0.521376    
## City..StateOrlando, FL       5.917e+00  6.277e+00   0.943 0.345995    
## City..StatePhiladelphia, PA  6.232e+00  6.189e+00   1.007 0.314084    
## City..StatePhoenix, AZ       2.406e+01  6.237e+00   3.857 0.000119 ***
## City..StatePittsburgh, PA   -2.853e+00  6.195e+00  -0.460 0.645262    
## City..StatePortland, OR      2.731e+01  6.295e+00   4.339 1.52e-05 ***
## City..StateRaleigh, NC      -3.007e+00  6.249e+00  -0.481 0.630426    
## City..StateSacramento, CA   -1.063e+00  6.206e+00  -0.171 0.864078    
## City..StateSeattle, WA       1.551e+01  6.160e+00   2.517 0.011913 *  
## City..StateTampa, FL         1.640e+01  6.214e+00   2.639 0.008392 ** 
## City..StateTucson, AZ        1.090e+01  6.223e+00   1.752 0.080037 .  
## City..StateTulsa, OK         4.952e+00  6.225e+00   0.795 0.426474    
## City..StateWashington, DC    1.243e+01  6.152e+00   2.020 0.043540 *  
## City..StateWichita, KS       1.576e+00  6.184e+00   0.255 0.798872    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 27.21 on 1657 degrees of freedom
## Multiple R-squared:  0.8088, Adjusted R-squared:  0.803 
## F-statistic: 140.2 on 50 and 1657 DF,  p-value: < 2.2e-16