nyc<-read.csv("C:\\Users\\dell\\Downloads\\nyc.csv")
attach(nyc)
cor(nyc)
## Price Food Decor Service East
## Price 1.0000000 0.6270435 0.72435248 0.6411402 0.18663024
## Food 0.6270435 1.0000000 0.50391610 0.7945248 0.18037061
## Decor 0.7243525 0.5039161 1.00000000 0.6453306 0.03574929
## Service 0.6411402 0.7945248 0.64533055 1.0000000 0.20909408
## East 0.1866302 0.1803706 0.03574929 0.2090941 1.00000000
plot(nyc)

m1<-lm(Price~Food+Decor+Service+East,data=nyc)
summary(m1)
##
## Call:
## lm(formula = Price ~ Food + Decor + Service + East, data = nyc)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.0465 -3.8837 0.0373 3.3942 17.7491
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -24.023800 4.708359 -5.102 9.24e-07 ***
## Food 1.538120 0.368951 4.169 4.96e-05 ***
## Decor 1.910087 0.217005 8.802 1.87e-15 ***
## Service -0.002727 0.396232 -0.007 0.9945
## East 2.068050 0.946739 2.184 0.0304 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.738 on 163 degrees of freedom
## Multiple R-squared: 0.6279, Adjusted R-squared: 0.6187
## F-statistic: 68.76 on 4 and 163 DF, p-value: < 2.2e-16
m2<-lm(Price~Service,data=nyc)
summary(m2)
##
## Call:
## lm(formula = Price ~ Service, data = nyc)
##
## Residuals:
## Min 1Q Median 3Q Max
## -17.6646 -4.7540 -0.2093 4.3368 26.2460
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -11.9778 5.1093 -2.344 0.0202 *
## Service 2.8184 0.2618 10.764 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.153 on 166 degrees of freedom
## Multiple R-squared: 0.4111, Adjusted R-squared: 0.4075
## F-statistic: 115.9 on 1 and 166 DF, p-value: < 2.2e-16
library(car)
## Loading required package: carData
influencePlot(m1)

## StudRes Hat CookD
## 30 2.9679503 0.01532064 0.026157895
## 56 3.2666518 0.05010858 0.106277650
## 117 0.4493433 0.20746530 0.010622954
## 130 2.9463084 0.07181092 0.128275446
## 168 0.4012884 0.21011533 0.008611493
m3<-lm(Price~Food+Decor+Service+East,data=nyc[-c(30,56,117,130,168)])
summary(m3)
##
## Call:
## lm(formula = Price ~ Food + Decor + Service + East, data = nyc[-c(30,
## 56, 117, 130, 168)])
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.0465 -3.8837 0.0373 3.3942 17.7491
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -24.023800 4.708359 -5.102 9.24e-07 ***
## Food 1.538120 0.368951 4.169 4.96e-05 ***
## Decor 1.910087 0.217005 8.802 1.87e-15 ***
## Service -0.002727 0.396232 -0.007 0.9945
## East 2.068050 0.946739 2.184 0.0304 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.738 on 163 degrees of freedom
## Multiple R-squared: 0.6279, Adjusted R-squared: 0.6187
## F-statistic: 68.76 on 4 and 163 DF, p-value: < 2.2e-16
vif(m1)
## Food Decor Service East
## 2.714261 1.744851 3.558735 1.064985
avPlots(m1)

finalmodel<-lm(Price~Food+Decor+East,data=nyc)
summary(finalmodel)
##
## Call:
## lm(formula = Price ~ Food + Decor + East, data = nyc)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.0451 -3.8809 0.0389 3.3918 17.7557
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -24.0269 4.6727 -5.142 7.67e-07 ***
## Food 1.5363 0.2632 5.838 2.76e-08 ***
## Decor 1.9094 0.1900 10.049 < 2e-16 ***
## East 2.0670 0.9318 2.218 0.0279 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.72 on 164 degrees of freedom
## Multiple R-squared: 0.6279, Adjusted R-squared: 0.6211
## F-statistic: 92.24 on 3 and 164 DF, p-value: < 2.2e-16
library(MASS)
stepAIC(m1)
## Start: AIC=591.95
## Price ~ Food + Decor + Service + East
##
## Df Sum of Sq RSS AIC
## - Service 1 0.0 5366.5 589.95
## <none> 5366.5 591.95
## - East 1 157.1 5523.6 594.79
## - Food 1 572.2 5938.7 606.97
## - Decor 1 2550.8 7917.3 655.28
##
## Step: AIC=589.95
## Price ~ Food + Decor + East
##
## Df Sum of Sq RSS AIC
## <none> 5366.5 589.95
## - East 1 161.0 5527.5 592.91
## - Food 1 1115.2 6481.7 619.67
## - Decor 1 3304.1 8670.6 668.55
##
## Call:
## lm(formula = Price ~ Food + Decor + East, data = nyc)
##
## Coefficients:
## (Intercept) Food Decor East
## -24.027 1.536 1.909 2.067
fm1<-lm(Price~log(Food)+log(Decor)+(East),data=nyc)
summary(fm1)
##
## Call:
## lm(formula = Price ~ log(Food) + log(Decor) + (East), data = nyc)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.3530 -3.6487 -0.0151 3.6583 18.2086
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -147.8229 14.2699 -10.359 < 2e-16 ***
## log(Food) 35.6554 5.2695 6.766 2.22e-10 ***
## log(Decor) 28.5854 2.9618 9.651 < 2e-16 ***
## East 1.7048 0.9486 1.797 0.0742 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.84 on 164 degrees of freedom
## Multiple R-squared: 0.6122, Adjusted R-squared: 0.6051
## F-statistic: 86.3 on 3 and 164 DF, p-value: < 2.2e-16
fm2<-lm(log(Price)~Food+Decor+East,data=nyc)
summary(fm2)
##
## Call:
## lm(formula = log(Price) ~ Food + Decor + East, data = nyc)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.36012 -0.08359 0.00410 0.08989 0.41077
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.121347 0.113971 18.613 < 2e-16 ***
## Food 0.034032 0.006419 5.302 3.66e-07 ***
## Decor 0.049276 0.004635 10.632 < 2e-16 ***
## East 0.055878 0.022728 2.459 0.015 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1395 on 164 degrees of freedom
## Multiple R-squared: 0.6331, Adjusted R-squared: 0.6264
## F-statistic: 94.35 on 3 and 164 DF, p-value: < 2.2e-16
fm3<-lm(Price~sqrt(Food)+sqrt(Decor)+sqrt(East),data=nyc)
summary(fm3)
##
## Call:
## lm(formula = Price ~ sqrt(Food) + sqrt(Decor) + sqrt(East), data = nyc)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.0159 -3.7658 0.1714 3.5283 17.7787
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -88.3980 9.3748 -9.429 < 2e-16 ***
## sqrt(Food) 14.6659 2.3532 6.232 3.74e-09 ***
## sqrt(Decor) 15.1263 1.5187 9.960 < 2e-16 ***
## sqrt(East) 1.8920 0.9358 2.022 0.0448 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.754 on 164 degrees of freedom
## Multiple R-squared: 0.6235, Adjusted R-squared: 0.6166
## F-statistic: 90.51 on 3 and 164 DF, p-value: < 2.2e-16
pred<-predict(fm2)
pred
## 1 2 3 4 5 6 7 8
## 3.757016 3.738228 3.476604 3.787504 3.874356 3.954120 3.658464 3.744830
## 9 10 11 12 13 14 15 16
## 3.862170 3.729586 3.661523 3.828138 3.828138 3.661523 3.744830 3.828138
## 17 18 19 20 21 22 23 24
## 3.812894 4.028786 3.661523 3.911446 3.945478 3.797650 3.744830 3.960722
## 25 26 27 28 29 30 31 32
## 3.911446 3.661523 3.862170 3.646278 3.862170 3.877414 3.646278 3.877414
## 33 34 35 36 37 38 39 40
## 3.794106 4.044030 4.112094 3.994754 3.778862 3.862170 3.979510 3.877414
## 41 42 43 44 45 46 47 48
## 3.661523 3.661523 3.729586 4.028786 3.763618 3.763618 3.597002 3.729586
## 49 50 51 52 53 54 55 56
## 3.880958 3.510151 3.744830 3.843383 3.710799 3.445631 3.528939 3.578215
## 57 58 59 60 61 62 63 64
## 3.797650 3.547726 3.763618 3.430386 3.979510 3.612246 3.578215 3.794106
## 65 66 67 68 69 70 71 72
## 3.562970 3.680310 3.794106 3.445631 3.642735 3.960722 3.812894 3.945478
## 73 74 75 76 77 78 79 80
## 3.979510 3.513694 3.945478 3.714342 3.680310 3.627491 3.578215 3.778862
## 81 82 83 84 85 86 87 88
## 3.778862 3.896202 3.896202 3.646278 3.896202 3.562970 3.846926 4.210646
## 89 90 91 92 93 94 95 96
## 3.828138 3.631034 3.812894 4.078062 3.748374 3.896202 3.710799 3.646278
## 97 98 99 100 101 102 103 104
## 3.464418 3.661523 3.513694 3.528939 3.710799 3.597002 3.748374 3.473061
## 105 106 107 108 109 110 111 112
## 4.028786 3.945478 3.578215 3.639676 3.794106 3.896202 3.612246 3.578215
## 113 114 115 116 117 118 119 120
## 3.945478 4.044030 3.233282 3.389753 3.131671 3.605644 3.507092 3.507092
## 121 122 123 124 125 126 127 128
## 3.605644 3.556368 3.488305 3.935333 3.639676 3.620889 3.757016 3.639676
## 129 130 131 132 133 134 135 136
## 3.541124 3.901301 3.802749 3.988152 3.639676 3.954120 3.738228 3.704197
## 137 138 139 140 141 142 143 144
## 3.654921 3.507092 3.491848 3.768717 3.722984 3.722984 4.052672 3.806292
## 145 146 147 148 149 150 151 152
## 4.101948 4.037428 3.753473 3.904844 3.673708 3.571613 3.624432 3.840324
## 153 154 155 156 157 158 159 160
## 3.772260 3.688952 3.389753 3.590400 3.806292 3.522337 3.404997 3.639676
## 161 162 163 164 165 166 167 168
## 3.772260 3.575156 3.556368 3.439029 3.590400 3.522337 3.707740 3.430871
pv<-as.data.frame(pred)
pv
## pred
## 1 3.757016
## 2 3.738228
## 3 3.476604
## 4 3.787504
## 5 3.874356
## 6 3.954120
## 7 3.658464
## 8 3.744830
## 9 3.862170
## 10 3.729586
## 11 3.661523
## 12 3.828138
## 13 3.828138
## 14 3.661523
## 15 3.744830
## 16 3.828138
## 17 3.812894
## 18 4.028786
## 19 3.661523
## 20 3.911446
## 21 3.945478
## 22 3.797650
## 23 3.744830
## 24 3.960722
## 25 3.911446
## 26 3.661523
## 27 3.862170
## 28 3.646278
## 29 3.862170
## 30 3.877414
## 31 3.646278
## 32 3.877414
## 33 3.794106
## 34 4.044030
## 35 4.112094
## 36 3.994754
## 37 3.778862
## 38 3.862170
## 39 3.979510
## 40 3.877414
## 41 3.661523
## 42 3.661523
## 43 3.729586
## 44 4.028786
## 45 3.763618
## 46 3.763618
## 47 3.597002
## 48 3.729586
## 49 3.880958
## 50 3.510151
## 51 3.744830
## 52 3.843383
## 53 3.710799
## 54 3.445631
## 55 3.528939
## 56 3.578215
## 57 3.797650
## 58 3.547726
## 59 3.763618
## 60 3.430386
## 61 3.979510
## 62 3.612246
## 63 3.578215
## 64 3.794106
## 65 3.562970
## 66 3.680310
## 67 3.794106
## 68 3.445631
## 69 3.642735
## 70 3.960722
## 71 3.812894
## 72 3.945478
## 73 3.979510
## 74 3.513694
## 75 3.945478
## 76 3.714342
## 77 3.680310
## 78 3.627491
## 79 3.578215
## 80 3.778862
## 81 3.778862
## 82 3.896202
## 83 3.896202
## 84 3.646278
## 85 3.896202
## 86 3.562970
## 87 3.846926
## 88 4.210646
## 89 3.828138
## 90 3.631034
## 91 3.812894
## 92 4.078062
## 93 3.748374
## 94 3.896202
## 95 3.710799
## 96 3.646278
## 97 3.464418
## 98 3.661523
## 99 3.513694
## 100 3.528939
## 101 3.710799
## 102 3.597002
## 103 3.748374
## 104 3.473061
## 105 4.028786
## 106 3.945478
## 107 3.578215
## 108 3.639676
## 109 3.794106
## 110 3.896202
## 111 3.612246
## 112 3.578215
## 113 3.945478
## 114 4.044030
## 115 3.233282
## 116 3.389753
## 117 3.131671
## 118 3.605644
## 119 3.507092
## 120 3.507092
## 121 3.605644
## 122 3.556368
## 123 3.488305
## 124 3.935333
## 125 3.639676
## 126 3.620889
## 127 3.757016
## 128 3.639676
## 129 3.541124
## 130 3.901301
## 131 3.802749
## 132 3.988152
## 133 3.639676
## 134 3.954120
## 135 3.738228
## 136 3.704197
## 137 3.654921
## 138 3.507092
## 139 3.491848
## 140 3.768717
## 141 3.722984
## 142 3.722984
## 143 4.052672
## 144 3.806292
## 145 4.101948
## 146 4.037428
## 147 3.753473
## 148 3.904844
## 149 3.673708
## 150 3.571613
## 151 3.624432
## 152 3.840324
## 153 3.772260
## 154 3.688952
## 155 3.389753
## 156 3.590400
## 157 3.806292
## 158 3.522337
## 159 3.404997
## 160 3.639676
## 161 3.772260
## 162 3.575156
## 163 3.556368
## 164 3.439029
## 165 3.590400
## 166 3.522337
## 167 3.707740
## 168 3.430871
final<-cbind(nyc,pv)