Title :“Phase 1” Author:“Muhammad Bilal” Date: “18-11-2023”
#assignment Phase 1 and 2
CamPriceData <- read.csv("D:/Sem 3/SI/data set prices of nikon and canon cameras updated.csv")
#attach the data
attach(CamPriceData)
#summary of the data
summary(CamPriceData)
## Observation Brand Price_. Megapixels
## Min. : 1.00 Length:28 Min. : 72.0 Min. :10.00
## 1st Qu.: 7.75 Class :character 1st Qu.: 99.0 1st Qu.:12.00
## Median :14.50 Mode :character Median :144.0 Median :12.00
## Mean :14.50 Mean :157.8 Mean :12.86
## 3rd Qu.:21.25 3rd Qu.:180.0 3rd Qu.:14.00
## Max. :28.00 Max. :360.0 Max. :16.00
## Weight_oz Score Brand.1
## Min. :4.000 Min. :51.00 Min. :0.0000
## 1st Qu.:5.000 1st Qu.:61.00 1st Qu.:0.0000
## Median :6.000 Median :65.50 Median :0.0000
## Mean :5.821 Mean :65.36 Mean :0.4643
## 3rd Qu.:7.000 3rd Qu.:70.25 3rd Qu.:1.0000
## Max. :7.000 Max. :75.00 Max. :1.0000
##DAta types: Brand: string Price_.: integer Megapixels: integer weight_oz: integer score: Integer Brand.1: Boolean
##Measurement scale: Brand:nominal Price_.: scaled Megapixels: scaled weight_oz: scaled score: scaled Brand.1: normal
#subsets for nikon and canon cameras
NikonPrice <- CamPriceData$Price_.[CamPriceData$Brand=="Nikon"]
CanonPrice <- CamPriceData$Price_.[CamPriceData$Brand=="Canon"]
str(CamPriceData)
## 'data.frame': 28 obs. of 7 variables:
## $ Observation: int 1 2 3 4 5 6 7 8 9 10 ...
## $ Brand : chr "Canon" "Canon" "Canon" "Canon" ...
## $ Price_. : int 297 180 270 180 162 180 180 117 117 99 ...
## $ Megapixels : int 10 12 12 10 12 12 14 10 12 16 ...
## $ Weight_oz : int 7 5 7 6 5 7 5 7 5 5 ...
## $ Score : int 75 75 74 71 71 70 69 69 68 64 ...
## $ Brand.1 : int 1 1 1 1 1 1 1 1 1 1 ...
#Checking normality using histogram
#histogram for Nikon camera
hist(NikonPrice, main = "Nikon Camera Prices")
#histogram for Canon Camera
hist(CanonPrice,main = "Canon Camera Prices")
#checking normality using QQ plot #qq plot for Nikon
qqnorm(NikonPrice, main="Normal")
#qq plot for canon
qqnorm(CanonPrice, main="Normal")
#data seems normal
summary(NikonPrice)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 72.0 103.5 135.0 159.0 193.5 360.0
summary(CanonPrice)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 81.0 99.0 162.0 156.5 180.0 297.0
#Testing Hypothesis # H0: Nikonprice is equal to Canonprice # H1: Nikonprice is not equal to Canonprice
#performing t test
t.test(NikonPrice,CanonPrice)
##
## Welch Two Sample t-test
##
## data: NikonPrice and CanonPrice
## t = 0.08931, df = 25.993, p-value = 0.9295
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -55.88673 60.96365
## sample estimates:
## mean of x mean of y
## 159.0000 156.4615
#data shows sufficient evidence to conclude that H0 is true. #accept the null hypothesis
#new variable ‘xweight’ from the data of ‘weight_oz’ data
CamPriceData$xweight <- ifelse(CamPriceData$Weight_oz %in% c(4,5), "light weight", ifelse(CamPriceData$Weight_oz ==6, "medium weight", ifelse(CamPriceData$Weight_oz==7, "heavy weight", NA)))
AnovaResult <- aov(Price_.~xweight, data=CamPriceData)
summary(AnovaResult)
## Df Sum Sq Mean Sq F value Pr(>F)
## xweight 2 16610 8305 1.557 0.23
## Residuals 25 133318 5333
#pvalue of 0.23 is greater than significant value #there isnt sufficient evidence to reject null hypothesis #hence the avg price of all weight category cameras is same
#Histogram and QQplots #histogram of price
hist(CamPriceData$Price_.)
#qqplot of price
qqnorm(CamPriceData$Price_.)
#Histogram of Megapixels
hist(CamPriceData$Megapixels)
#QQplot of Megapixels
qqnorm(CamPriceData$Megapixels)
#Histogram for Weight
hist(CamPriceData$Weight_oz)
#QQplot for Weight
qqnorm(CamPriceData$Weight_oz)
#Histogram for score
hist(CamPriceData$Score)
#QQplot for score
qqnorm(CamPriceData$Score)
#Scatter plots matrix
pairs(~CamPriceData$Price_. + CamPriceData$Megapixels + CamPriceData$Weight_oz + CamPriceData$Score, data=CamPriceData)
#Correlation matrix
cor_data <- data.frame(CamPriceData$Price_., CamPriceData$Megapixels, CamPriceData$Weight_oz, CamPriceData$Score)
cor(cor_data)
## CamPriceData.Price_. CamPriceData.Megapixels
## CamPriceData.Price_. 1.0000000 0.138906307
## CamPriceData.Megapixels 0.1389063 1.000000000
## CamPriceData.Weight_oz 0.3488151 -0.198833809
## CamPriceData.Score 0.6832118 -0.007729723
## CamPriceData.Weight_oz CamPriceData.Score
## CamPriceData.Price_. 0.3488151 0.683211844
## CamPriceData.Megapixels -0.1988338 -0.007729723
## CamPriceData.Weight_oz 1.0000000 0.285688204
## CamPriceData.Score 0.2856882 1.000000000
#Regression model to predict using Price using Megapixels
m1 <- lm(CamPriceData$Price_. ~ CamPriceData$Megapixels)
m1
##
## Call:
## lm(formula = CamPriceData$Price_. ~ CamPriceData$Megapixels)
##
## Coefficients:
## (Intercept) CamPriceData$Megapixels
## 85.500 5.625
summary(m1)
##
## Call:
## lm(formula = CamPriceData$Price_. ~ CamPriceData$Megapixels)
##
## Residuals:
## Min 1Q Median 3Q Max
## -92.25 -54.56 -20.25 29.81 195.75
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 85.500 102.112 0.837 0.410
## CamPriceData$Megapixels 5.625 7.865 0.715 0.481
##
## Residual standard error: 75.2 on 26 degrees of freedom
## Multiple R-squared: 0.01929, Adjusted R-squared: -0.01842
## F-statistic: 0.5115 on 1 and 26 DF, p-value: 0.4808
#Reg model -> price= 85.5 + 5.625megapixels #The model is -1.842% efficient in predicting a change in price given a change in megapixels
#Regression model to predict using Price using Megapixels and Weight
m2 <- lm(CamPriceData$Price_. ~ CamPriceData$Megapixels + CamPriceData$Weight_oz)
m2
##
## Call:
## lm(formula = CamPriceData$Price_. ~ CamPriceData$Megapixels +
## CamPriceData$Weight_oz)
##
## Coefficients:
## (Intercept) CamPriceData$Megapixels CamPriceData$Weight_oz
## -127.975 8.781 29.701
summary(m2)
##
## Call:
## lm(formula = CamPriceData$Price_. ~ CamPriceData$Megapixels +
## CamPriceData$Weight_oz)
##
## Residuals:
## Min 1Q Median 3Q Max
## -130.861 -40.958 -1.029 37.896 157.139
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -127.975 139.666 -0.916 0.3683
## CamPriceData$Megapixels 8.781 7.543 1.164 0.2554
## CamPriceData$Weight_oz 29.701 14.116 2.104 0.0456 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 70.69 on 25 degrees of freedom
## Multiple R-squared: 0.1668, Adjusted R-squared: 0.1002
## F-statistic: 2.503 on 2 and 25 DF, p-value: 0.1021
#Reg model -> price= -127.975 + 8.781megapixels + 29.701weight #model m2 is 10.02% efficient in predicting a change in Price
#Regression model to predict using Price using Megapixels, Weight and Score.
m3 <- lm(CamPriceData$Price_. ~ CamPriceData$Megapixels + CamPriceData$Weight_oz + CamPriceData$Score)
m3
##
## Call:
## lm(formula = CamPriceData$Price_. ~ CamPriceData$Megapixels +
## CamPriceData$Weight_oz + CamPriceData$Score)
##
## Coefficients:
## (Intercept) CamPriceData$Megapixels CamPriceData$Weight_oz
## -484.703 7.486 15.676
## CamPriceData$Score
## 6.962
summary(m3)
##
## Call:
## lm(formula = CamPriceData$Price_. ~ CamPriceData$Megapixels +
## CamPriceData$Weight_oz + CamPriceData$Score)
##
## Residuals:
## Min 1Q Median 3Q Max
## -68.59 -31.48 -12.88 33.19 156.75
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -484.703 136.395 -3.554 0.001613 **
## CamPriceData$Megapixels 7.486 5.820 1.286 0.210573
## CamPriceData$Weight_oz 15.676 11.364 1.379 0.180467
## CamPriceData$Score 6.962 1.636 4.256 0.000275 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 54.46 on 24 degrees of freedom
## Multiple R-squared: 0.5252, Adjusted R-squared: 0.4659
## F-statistic: 8.85 on 3 and 24 DF, p-value: 0.0003961
#Reg model -> price= -484.703 + 7.486megapixels + 15.676weight + 6.962score #The model is 46.59% efficient in predicitng a change in price due to a change in megapixels, weight and score.
#Anova testing of Reg models
#H0: Both models are the same #H1: Both models are different
anovaX <- anova(m1,m3)
anovaX
## Analysis of Variance Table
##
## Model 1: CamPriceData$Price_. ~ CamPriceData$Megapixels
## Model 2: CamPriceData$Price_. ~ CamPriceData$Megapixels + CamPriceData$Weight_oz +
## CamPriceData$Score
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 26 147035
## 2 24 71184 2 75852 12.787 0.0001658 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(anovaX)
## Res.Df RSS Df Sum of Sq F
## Min. :24.0 Min. : 71184 Min. :2 Min. :75852 Min. :12.79
## 1st Qu.:24.5 1st Qu.: 90147 1st Qu.:2 1st Qu.:75852 1st Qu.:12.79
## Median :25.0 Median :109109 Median :2 Median :75852 Median :12.79
## Mean :25.0 Mean :109109 Mean :2 Mean :75852 Mean :12.79
## 3rd Qu.:25.5 3rd Qu.:128072 3rd Qu.:2 3rd Qu.:75852 3rd Qu.:12.79
## Max. :26.0 Max. :147035 Max. :2 Max. :75852 Max. :12.79
## NA's :1 NA's :1 NA's :1
## Pr(>F)
## Min. :0.0001658
## 1st Qu.:0.0001658
## Median :0.0001658
## Mean :0.0001658
## 3rd Qu.:0.0001658
## Max. :0.0001658
## NA's :1
#Since the P value is smaller than the significance level, the data provides sufficient evidence to conclude that the models differ from eachother
#H0: Both models are the same #H1: Both models are different
AnovaY <- anova(m2, m3)
AnovaY
## Analysis of Variance Table
##
## Model 1: CamPriceData$Price_. ~ CamPriceData$Megapixels + CamPriceData$Weight_oz
## Model 2: CamPriceData$Price_. ~ CamPriceData$Megapixels + CamPriceData$Weight_oz +
## CamPriceData$Score
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 25 124916
## 2 24 71184 1 53732 18.116 0.0002752 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(AnovaY)
## Res.Df RSS Df Sum of Sq F
## Min. :24.00 Min. : 71184 Min. :1 Min. :53732 Min. :18.12
## 1st Qu.:24.25 1st Qu.: 84617 1st Qu.:1 1st Qu.:53732 1st Qu.:18.12
## Median :24.50 Median : 98050 Median :1 Median :53732 Median :18.12
## Mean :24.50 Mean : 98050 Mean :1 Mean :53732 Mean :18.12
## 3rd Qu.:24.75 3rd Qu.:111483 3rd Qu.:1 3rd Qu.:53732 3rd Qu.:18.12
## Max. :25.00 Max. :124916 Max. :1 Max. :53732 Max. :18.12
## NA's :1 NA's :1 NA's :1
## Pr(>F)
## Min. :0.0002752
## 1st Qu.:0.0002752
## Median :0.0002752
## Mean :0.0002752
## 3rd Qu.:0.0002752
## Max. :0.0002752
## NA's :1
#Since the P value is smaller than the significance level, the data provides sufficient evidence to conclude that the models differ from eachother
#Variable for Nikon
CamPriceData$Nikon <- ifelse(CamPriceData$Brand.1 == 0,1,0)
print(CamPriceData)
## Observation Brand Price_. Megapixels Weight_oz Score Brand.1 xweight
## 1 1 Canon 297 10 7 75 1 heavy weight
## 2 2 Canon 180 12 5 75 1 light weight
## 3 3 Canon 270 12 7 74 1 heavy weight
## 4 4 Canon 180 10 6 71 1 medium weight
## 5 5 Canon 162 12 5 71 1 light weight
## 6 6 Canon 180 12 7 70 1 heavy weight
## 7 7 Canon 180 14 5 69 1 light weight
## 8 8 Canon 117 10 7 69 1 heavy weight
## 9 9 Canon 117 12 5 68 1 light weight
## 10 10 Canon 99 16 5 64 1 light weight
## 11 11 Canon 81 14 5 61 1 light weight
## 12 12 Canon 90 10 6 60 1 medium weight
## 13 13 Canon 81 12 7 55 1 heavy weight
## 14 14 Nikon 243 16 5 74 0 light weight
## 15 15 Nikon 270 16 7 72 0 heavy weight
## 16 16 Nikon 180 14 6 70 0 medium weight
## 17 17 Nikon 360 14 7 68 0 heavy weight
## 18 18 Nikon 108 14 5 66 0 light weight
## 19 19 Nikon 153 16 6 65 0 medium weight
## 20 20 Nikon 135 12 5 65 0 light weight
## 21 21 Nikon 207 14 6 64 0 medium weight
## 22 22 Nikon 162 12 6 62 0 medium weight
## 23 23 Nikon 117 12 6 62 0 medium weight
## 24 24 Nikon 72 12 7 61 0 heavy weight
## 25 25 Nikon 72 14 7 59 0 heavy weight
## 26 26 Nikon 90 12 4 55 0 light weight
## 27 27 Nikon 99 12 5 54 0 light weight
## 28 28 Nikon 117 14 4 51 0 light weight
## Nikon
## 1 0
## 2 0
## 3 0
## 4 0
## 5 0
## 6 0
## 7 0
## 8 0
## 9 0
## 10 0
## 11 0
## 12 0
## 13 0
## 14 1
## 15 1
## 16 1
## 17 1
## 18 1
## 19 1
## 20 1
## 21 1
## 22 1
## 23 1
## 24 1
## 25 1
## 26 1
## 27 1
## 28 1
m4 <- lm(CamPriceData$Price_. ~ CamPriceData$Megapixels + CamPriceData$Weight_oz + CamPriceData$Score + CamPriceData$Nikon)
m4
##
## Call:
## lm(formula = CamPriceData$Price_. ~ CamPriceData$Megapixels +
## CamPriceData$Weight_oz + CamPriceData$Score + CamPriceData$Nikon)
##
## Coefficients:
## (Intercept) CamPriceData$Megapixels CamPriceData$Weight_oz
## -502.992 2.626 13.671
## CamPriceData$Score CamPriceData$Nikon
## 8.062 38.388
summary(m4)
##
## Call:
## lm(formula = CamPriceData$Price_. ~ CamPriceData$Megapixels +
## CamPriceData$Weight_oz + CamPriceData$Score + CamPriceData$Nikon)
##
## Residuals:
## Min 1Q Median 3Q Max
## -82.379 -28.677 -3.307 22.454 143.936
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -502.992 132.893 -3.785 0.000958 ***
## CamPriceData$Megapixels 2.626 6.438 0.408 0.687152
## CamPriceData$Weight_oz 13.671 11.103 1.231 0.230666
## CamPriceData$Score 8.062 1.735 4.648 0.000112 ***
## CamPriceData$Nikon 38.388 24.395 1.574 0.129228
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 52.86 on 23 degrees of freedom
## Multiple R-squared: 0.5714, Adjusted R-squared: 0.4968
## F-statistic: 7.665 on 4 and 23 DF, p-value: 0.0004448
#Reg model -> price= -502.992 + 2.626megapixels + 13.671weight + 8.062score + 38.388nikon #This model is 49.68% efficient in predicting changes in Price.
#Reg model for Nikon
m4Nik <- lm(CamPriceData$Price_.[CamPriceData$Nikon==1] ~ CamPriceData$Megapixels[CamPriceData$Nikon==1] + CamPriceData$Weight_oz[CamPriceData$Nikon==1] + CamPriceData$Score[CamPriceData$Nikon==1] )
m4Nik
##
## Call:
## lm(formula = CamPriceData$Price_.[CamPriceData$Nikon == 1] ~
## CamPriceData$Megapixels[CamPriceData$Nikon == 1] + CamPriceData$Weight_oz[CamPriceData$Nikon ==
## 1] + CamPriceData$Score[CamPriceData$Nikon == 1])
##
## Coefficients:
## (Intercept)
## -434.049
## CamPriceData$Megapixels[CamPriceData$Nikon == 1]
## 11.153
## CamPriceData$Weight_oz[CamPriceData$Nikon == 1]
## 6.284
## CamPriceData$Score[CamPriceData$Nikon == 1]
## 6.414
summary(m4Nik)
##
## Call:
## lm(formula = CamPriceData$Price_.[CamPriceData$Nikon == 1] ~
## CamPriceData$Megapixels[CamPriceData$Nikon == 1] + CamPriceData$Weight_oz[CamPriceData$Nikon ==
## 1] + CamPriceData$Score[CamPriceData$Nikon == 1])
##
## Residuals:
## Min 1Q Median 3Q Max
## -72.483 -37.368 -7.426 24.162 157.794
##
## Coefficients:
## Estimate Std. Error t value
## (Intercept) -434.049 185.267 -2.343
## CamPriceData$Megapixels[CamPriceData$Nikon == 1] 11.153 13.975 0.798
## CamPriceData$Weight_oz[CamPriceData$Nikon == 1] 6.284 18.952 0.332
## CamPriceData$Score[CamPriceData$Nikon == 1] 6.414 3.598 1.783
## Pr(>|t|)
## (Intercept) 0.039 *
## CamPriceData$Megapixels[CamPriceData$Nikon == 1] 0.442
## CamPriceData$Weight_oz[CamPriceData$Nikon == 1] 0.746
## CamPriceData$Score[CamPriceData$Nikon == 1] 0.102
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 65.54 on 11 degrees of freedom
## Multiple R-squared: 0.4907, Adjusted R-squared: 0.3518
## F-statistic: 3.532 on 3 and 11 DF, p-value: 0.05199
#Regression model -> Price = -434.049 + 11.153Megapixels + 6.284Weight + 6.414score #The model is 35.18% efficient in predicting the changes in price for Nikon
#Regression model for Canon
m4can <- lm(CamPriceData$Price_.[CamPriceData$Nikon==0] ~ CamPriceData$Megapixels[CamPriceData$Nikon==0] + CamPriceData$Weight_oz[CamPriceData$Nikon==0] + CamPriceData$Score[CamPriceData$Nikon==0])
m4can
##
## Call:
## lm(formula = CamPriceData$Price_.[CamPriceData$Nikon == 0] ~
## CamPriceData$Megapixels[CamPriceData$Nikon == 0] + CamPriceData$Weight_oz[CamPriceData$Nikon ==
## 0] + CamPriceData$Score[CamPriceData$Nikon == 0])
##
## Coefficients:
## (Intercept)
## -732.616
## CamPriceData$Megapixels[CamPriceData$Nikon == 0]
## 5.728
## CamPriceData$Weight_oz[CamPriceData$Nikon == 0]
## 29.677
## CamPriceData$Score[CamPriceData$Nikon == 0]
## 9.500
summary(m4can)
##
## Call:
## lm(formula = CamPriceData$Price_.[CamPriceData$Nikon == 0] ~
## CamPriceData$Megapixels[CamPriceData$Nikon == 0] + CamPriceData$Weight_oz[CamPriceData$Nikon ==
## 0] + CamPriceData$Score[CamPriceData$Nikon == 0])
##
## Residuals:
## Min 1Q Median 3Q Max
## -70.93 -16.44 2.97 17.25 52.07
##
## Coefficients:
## Estimate Std. Error t value
## (Intercept) -732.616 207.021 -3.539
## CamPriceData$Megapixels[CamPriceData$Nikon == 0] 5.728 7.103 0.806
## CamPriceData$Weight_oz[CamPriceData$Nikon == 0] 29.677 13.165 2.254
## CamPriceData$Score[CamPriceData$Nikon == 0] 9.500 1.724 5.509
## Pr(>|t|)
## (Intercept) 0.006325 **
## CamPriceData$Megapixels[CamPriceData$Nikon == 0] 0.440761
## CamPriceData$Weight_oz[CamPriceData$Nikon == 0] 0.050653 .
## CamPriceData$Score[CamPriceData$Nikon == 0] 0.000376 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 35.32 on 9 degrees of freedom
## Multiple R-squared: 0.8034, Adjusted R-squared: 0.7379
## F-statistic: 12.26 on 3 and 9 DF, p-value: 0.001568
#Reg model -> price = -732.616 + 5.728megapixels + 29.677weight + 9.5score #This model is 73.79% efficient in predicting changes in price for canon.
#Graphs of the regression model of Nikon
plot(m4Nik)
#Graphs of the regression model of Canon
plot(m4can)