Title: “Phase 2”

Author: “Shariqa Bahalim 26070”

Date: “2023-11-25”

Output: html_document

LOADING THE DATA FILE IN R

NCanon <- read.csv("C:/Users/Shariqa/Downloads/NC.csv")
attach(NCanon)
summary(NCanon)
##   Observation       Brand              Price_.        Megapixels   
##  Min.   : 1.00   Length:28          Min.   : 8.00   Min.   :10.00  
##  1st Qu.: 7.75   Class :character   1st Qu.:11.00   1st Qu.:12.00  
##  Median :14.50   Mode  :character   Median :16.00   Median :12.00  
##  Mean   :14.50                      Mean   :17.54   Mean   :12.86  
##  3rd Qu.:21.25                      3rd Qu.:20.00   3rd Qu.:14.00  
##  Max.   :28.00                      Max.   :40.00   Max.   :16.00  
##    Weight_oz         Score          Brand.1      
##  Min.   :4.000   Min.   :49.00   Min.   :0.0000  
##  1st Qu.:5.000   1st Qu.:59.00   1st Qu.:0.0000  
##  Median :6.000   Median :63.50   Median :0.0000  
##  Mean   :5.821   Mean   :63.36   Mean   :0.4643  
##  3rd Qu.:7.000   3rd Qu.:68.25   3rd Qu.:1.0000  
##  Max.   :7.000   Max.   :73.00   Max.   :1.0000
str(NCanon)
## '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  33 20 30 20 18 20 20 13 13 11 ...
##  $ 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  73 73 72 69 69 68 67 67 66 62 ...
##  $ Brand.1    : int  1 1 1 1 1 1 1 1 1 1 ...

BOXPLOTS OF EACH INTEGER VARIABLE

boxplot(NCanon $Price, horizontal = TRUE, xlab= "Price")

The boxplot of the variable “Price” is positioned in the 1st quartile, and there seems to be 1 outlier value.

boxplot(NCanon $Megapixels, horizontal = TRUE, xlab= "Megapixels")

The boxplot of the variable “Megapixels” is positioned in the middle; however, the median of this variable is closer to its 1st quartile.

boxplot(NCanon $Weight_oz, horizontal = TRUE, xlab= "Weight")

The boxplot of the variable “Weight” is positioned more broadly towards the 3rd quartile.

boxplot(NCanon$Score, horizontal = TRUE, xlab= "Score")

The boxplot of the variable “Score” is positioned closer to the 3rd quartile.

PERFORMING THE T-TEST

nikon_prices <- NCanon $Price [NCanon $Brand == "Nikon"]
canon_prices <- NCanon $Price [NCanon $Brand == "Canon"]
t_test_result <- t.test(nikon_prices, canon_prices)
print(t_test_result)
## 
##  Welch Two Sample t-test
## 
## data:  nikon_prices and canon_prices
## 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:
##  -6.209637  6.773739
## sample estimates:
## mean of x mean of y 
##  17.66667  17.38462

It seems that the p-value (0.9295) is greater than the significance level; therefore, we fail to reject the null hypothesis.

NEW VARIABLE WEIGHT CATEGORY

NCanon $Weight_Category <- cut(
    NCanon $Weight_oz,
  breaks = c(-Inf, 4, 5, 6, 7, Inf),
    labels = c("Other", "Light Weight", "Light Weight", "Medium Weight", "Heavy Weight"),
    include.lowest = TRUE
  )
head(NCanon)
##   Observation Brand Price_. Megapixels Weight_oz Score Brand.1 Weight_Category
## 1           1 Canon      33         10         7    73       1   Medium Weight
## 2           2 Canon      20         12         5    73       1    Light Weight
## 3           3 Canon      30         12         7    72       1   Medium Weight
## 4           4 Canon      20         10         6    69       1    Light Weight
## 5           5 Canon      18         12         5    69       1    Light Weight
## 6           6 Canon      20         12         7    68       1   Medium Weight

Categorized the weights into different categories with the labels “Light weights = 4,5”, “Medium weight = 6”, “Heavy weight = 7”.

ONE-WAY ANOVA TEST

anova_result <- aov(NCanon $Price ~ NCanon $Weight_Category, data = NCanon) 
summary(anova_result)
##                        Df Sum Sq Mean Sq F value Pr(>F)
## NCanon$Weight_Category  2  221.4  110.69   1.698  0.203
## Residuals              25 1629.6   65.18

The p-value (0.203) is greater than 0.05, indicating that there is not enough evidence to reject the null hypothesis. Therefore, we do not have sufficient evidence to conclude that the average prices are different for different weight categories.

HISTOGRAMS

hist(NCanon $Price, col=3)

It appears to be Right-Skewed.

hist(Megapixels, col=4)

It appears to be Right-Skewed.

hist(Weight_oz, col=5)

It appears not to be normal.

hist(Score, col=6)

It appears to be left-skewed.

MATRIX SCATTER PLOT

df <- data.frame(NCanon$Price, Megapixels, Weight_oz, Score)
plot(df)

The only approximately normal scatter plot seems to be the plots of Price and Score. The scatter plot is positively linear when Price is on the x-axis and the graph is negative in linearity when Price is on the y-axis.

CORRELATION

cor(df)
##              NCanon.Price   Megapixels  Weight_oz        Score
## NCanon.Price    1.0000000  0.138906307  0.3488151  0.683211844
## Megapixels      0.1389063  1.000000000 -0.1988338 -0.007729723
## Weight_oz       0.3488151 -0.198833809  1.0000000  0.285688204
## Score           0.6832118 -0.007729723  0.2856882  1.000000000
NCanon.Price and Megapixels (0.14):

The correlation between the price of cameras and megapixels is positive but relatively low (0.14). This suggests a weak positive linear relationship; as the megapixels increase, there’s a slight tendency for the price to increase.

NCanon.Price and Weight_oz (0.35):

The correlation between the price of cameras and their weight is moderately positive (0.35). This indicates that there is a moderate tendency for heavier cameras to have higher prices.

NCanon.Price and Score (0.68):

The correlation between the price of cameras and their score is strong and positive (0.68). This suggests a substantial positive linear relationship; cameras with higher scores tend to have higher prices.

Megapixels and Weight_oz (-0.20):

The correlation between megapixels and weight is weakly negative (-0.20). This implies a slight tendency for heavier cameras to have fewer megapixels, although the relationship is not very strong.

Megapixels and Score (-0.01):

The correlation between megapixels and the score is very close to zero (-0.01), suggesting little to no linear relationship between these two variables.

Weight_oz and Score (0.29):

The correlation between weight and score is positive (0.29), indicating a moderate positive linear relationship. Heavier cameras tend to have slightly higher scores.

REGRESSION MODEL M1

m1 <- lm(NCanon $Price ~ Megapixels)
summary(m1)
## 
## Call:
## lm(formula = NCanon$Price ~ Megapixels)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -10.250  -6.062  -2.250   3.312  21.750 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   9.5000    11.3457   0.837    0.410
## Megapixels    0.6250     0.8739   0.715    0.481
## 
## Residual standard error: 8.356 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

The model m1 is a simple linear regression model with Price as the dependent variable and Megapixels as the independent variable.
The coefficient for Megapixels is 0.6250, suggesting that, on average, a one-unit increase in Megapixels is associated with a 0.6250 unit increase in Price.
The Adjusted R^2 is very low (0.01929), indicating that Megapixels alone explains a small proportion of the variability in Price.
The p-value for Megapixels is 0.4808, suggesting that Megapixels is not a statistically significant predictor of Price in this model.

REGRESSION MODEL M2

m2 <- lm(NCanon $Price ~ Megapixels + Weight_oz)
summary(m2)
## 
## Call:
## lm(formula = NCanon$Price ~ Megapixels + Weight_oz)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -14.5401  -4.5509  -0.1143   4.2107  17.4599 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -14.2195    15.5185  -0.916   0.3683  
## Megapixels    0.9756     0.8381   1.164   0.2554  
## Weight_oz     3.3001     1.5685   2.104   0.0456 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.854 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

Model m2 adds Weight_oz as an additional predictor to the Megapixels model.
The Adjusted R^2 improves slightly (0.1668), suggesting that the combination of Megapixels and Weight_oz explains more variability in Price than Megapixels alone.
The p-value for Weight_oz is 0.0456, indicating that Weight_oz is marginally statistically significant in predicting Price in this model.

REGRESSION MODEL M3

m3 <- lm(NCanon $Price ~ Megapixels + Weight_oz + Score)
summary(m3)
## 
## Call:
## lm(formula = NCanon$Price ~ Megapixels + Weight_oz + Score)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -7.622 -3.498 -1.431  3.688 17.416 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -52.3087    14.9344  -3.503 0.001831 ** 
## Megapixels    0.8318     0.6466   1.286 0.210573    
## Weight_oz     1.7418     1.2627   1.379 0.180467    
## Score         0.7735     0.1817   4.256 0.000275 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.051 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

Model m3 further adds the variable Score as a predictor.
The Adjusted R^2 increases substantially (0.5252), suggesting that the combination of Megapixels, Weight_oz, and Score explains a significant amount of variability in Price.
The p-values for Megapixels, Weight_oz, and Score are all statistically significant (p-values < 0.05), indicating that all three predictors are contributing significantly to the model.

COMPARISON OF M1 AND M3 USING ANOVA

anova(m1, m3)
## Analysis of Variance Table
## 
## Model 1: NCanon$Price ~ Megapixels
## Model 2: NCanon$Price ~ Megapixels + Weight_oz + Score
##   Res.Df     RSS Df Sum of Sq      F    Pr(>F)    
## 1     26 1815.25                                  
## 2     24  878.81  2    936.44 12.787 0.0001658 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The ANOVA results show a statistically significant difference between m1 and m3 (p-value = 0.0003961), suggesting that m3, with Megapixels, Weight_oz, and Score, is a better fit than m1. The Adjusted R^2 for m3 is much higher than for m1, indicating that the additional predictors in m3 improve the model’s ability to explain the variability in Price.

COMPARISON OF M2 AND M3 USING ANOVA

anova(m2, m3)
## Analysis of Variance Table
## 
## Model 1: NCanon$Price ~ Megapixels + Weight_oz
## Model 2: NCanon$Price ~ Megapixels + Weight_oz + Score
##   Res.Df     RSS Df Sum of Sq      F    Pr(>F)    
## 1     25 1542.17                                  
## 2     24  878.81  1    663.36 18.116 0.0002752 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The ANOVA results also show a statistically significant difference between m2 and m3 (p-value = 0.0002752), indicating that m3 is a better fit than m2. The Adjusted R^2 for m3 is higher than for m2, indicating that the addition of Score in m3 improves the model’s performance.

ADDING INDICATOR VARIABLE “NIKON”

NCanon$Nikon <- ifelse(Brand == "Nikon", 1, 0)
str(NCanon)
## 'data.frame':    28 obs. of  9 variables:
##  $ Observation    : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Brand          : chr  "Canon" "Canon" "Canon" "Canon" ...
##  $ Price_.        : int  33 20 30 20 18 20 20 13 13 11 ...
##  $ 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  73 73 72 69 69 68 67 67 66 62 ...
##  $ Brand.1        : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ Weight_Category: Factor w/ 4 levels "Other","Light Weight",..: 3 2 3 2 2 3 2 3 2 2 ...
##  $ Nikon          : num  0 0 0 0 0 0 0 0 0 0 ...

added the indicator variable based on “Brand”.

REGRESSION MODEL M4

m4 <- lm(NCanon$Price ~ Megapixels + Weight_oz + Score + NCanon$Nikon)
summary(m4)
## 
## Call:
## lm(formula = NCanon$Price ~ Megapixels + Weight_oz + Score + 
##     NCanon$Nikon)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.1532 -3.1863 -0.3674  2.4949 15.9929 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -54.0964    14.5396  -3.721 0.001123 ** 
## Megapixels     0.2917     0.7153   0.408 0.687152    
## Weight_oz      1.5190     1.2337   1.231 0.230666    
## Score          0.8958     0.1927   4.648 0.000112 ***
## NCanon$Nikon   4.2654     2.7105   1.574 0.129228    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.873 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
plot(m4)

The Adjusted R^2 for m4 is 0.4968, indicating that the model explains a substantial proportion of the variability in Price.
Megapixels and Score are statistically significant predictors (p < 0.05).
Weight_oz and Nikon are not statistically significant at the 0.05 level, though Weight_oz is marginally significant.

FITTED REGRESSION LINE FOR NIKON AND CANON

Fitted Regression line for Nikon:
y= -54.0964 + 0.2917(B1) + 1.5190(B2) + 0.8958(B3) + 4.2654(B4=1)

Fitted Regression line for Canon
y= -54.0964 + 0.2917(B1) + 1.5190(B2) + 0.8958(B3) + 4.2654(B4=0)

THE END