This is my First R-Markdown document

This is Qamar roll number 04 Section c

## (a) Load the data file in R
cameradataset <- read.csv("E:/cameradataset.csv")



## (b) Describe the data, type, and measurement scale of all variables
summary(cameradataset)  
##   Observation       Brand              Price_.        Megapixels   
##  Min.   : 1.00   Length:28          Min.   : 80.5   Min.   :10.00  
##  1st Qu.: 7.75   Class :character   1st Qu.:110.5   1st Qu.:12.00  
##  Median :14.50   Mode  :character   Median :160.5   Median :12.00  
##  Mean   :14.50                      Mean   :175.9   Mean   :12.86  
##  3rd Qu.:21.25                      3rd Qu.:200.5   3rd Qu.:14.00  
##  Max.   :28.00                      Max.   :400.5   Max.   :16.00  
##    Weight_oz         Score          Brand.1      
##  Min.   :4.000   Min.   :46.00   Min.   :0.0000  
##  1st Qu.:5.000   1st Qu.:56.00   1st Qu.:0.0000  
##  Median :6.000   Median :60.50   Median :0.0000  
##  Mean   :5.821   Mean   :60.36   Mean   :0.4643  
##  3rd Qu.:7.000   3rd Qu.:65.25   3rd Qu.:1.0000  
##  Max.   :7.000   Max.   :70.00   Max.   :1.0000
str(cameradataset)  
## '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_.    : num  330 200 300 200 180 ...
##  $ 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  70 70 69 66 66 65 64 64 63 59 ...
##  $ Brand.1    : int  1 1 1 1 1 1 1 1 1 1 ...
par(mar = c(5, 5, 2, 2))

## (c) Make the histogram of all scaled variables and check the normality
hist(cameradataset$Price, main = "Histogram of Price", col = "blue", xlab = "Price")

hist(cameradataset$Megapixels, main = "Histogram of Megapixels", col = "green", xlab = "Megapixels")

hist(cameradataset$Weight, main = "Histogram of Weight", col = "red", xlab = "Weight")

hist(cameradataset$Score, main = "Histogram of Score", col = "orange", xlab = "Score")

## Shapiro-Wilk test for normality
shapiro.test(cameradataset$Price)
## 
##  Shapiro-Wilk normality test
## 
## data:  cameradataset$Price
## W = 0.89739, p-value = 0.009945
shapiro.test(cameradataset$Megapixels)
## 
##  Shapiro-Wilk normality test
## 
## data:  cameradataset$Megapixels
## W = 0.87756, p-value = 0.003549
shapiro.test(cameradataset$Weight)
## 
##  Shapiro-Wilk normality test
## 
## data:  cameradataset$Weight
## W = 0.84974, p-value = 0.0009235
shapiro.test(cameradataset$Score)
## 
##  Shapiro-Wilk normality test
## 
## data:  cameradataset$Score
## W = 0.95719, p-value = 0.2985
## Matrix scatter plot
names(cameradataset)
## [1] "Observation" "Brand"       "Price_."     "Megapixels"  "Weight_oz"  
## [6] "Score"       "Brand.1"
pairs(cameradataset[, c("Price_.", "Megapixels", "Weight_oz", "Score")])

## Correlation matrix
cor_matrix <- cor(cameradataset[, c("Price_.", "Megapixels", "Weight_oz", "Score")])
print(cor_matrix)
##              Price_.   Megapixels  Weight_oz        Score
## 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
## Fit a regression model m1
m1 <- lm(Price_. ~ Megapixels, data = cameradataset)
summary(m1)
## 
## Call:
## lm(formula = Price_. ~ Megapixels, data = cameradataset)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -102.50  -60.62  -22.50   33.12  217.50 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   95.500    113.457   0.842    0.408
## Megapixels     6.250      8.739   0.715    0.481
## 
## Residual standard error: 83.56 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
## Residual analysis
plot(m1)

#3 Fit a regression model m2
m2 <- lm(Price_. ~ Megapixels + Weight_oz, data = cameradataset)

## Summary of the model
summary(m2)
## 
## Call:
## lm(formula = Price_. ~ Megapixels + Weight_oz, data = cameradataset)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -145.401  -45.509   -1.143   42.107  174.599 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -141.695    155.185  -0.913   0.3699  
## Megapixels     9.756      8.381   1.164   0.2554  
## Weight_oz     33.001     15.685   2.104   0.0456 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 78.54 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
## Residual analysis
plot(m2)

# Fit a regression model m3
m3 <- lm(Price_. ~ Megapixels + Weight_oz + Score, data = cameradataset)

## Summary of the model
summary(m3)
## 
## Call:
## lm(formula = Price_. ~ Megapixels + Weight_oz + Score, data = cameradataset)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -76.22 -34.98 -14.31  36.88 174.16 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -499.381    146.142  -3.417 0.002261 ** 
## Megapixels     8.318      6.466   1.286 0.210573    
## Weight_oz     17.418     12.627   1.379 0.180467    
## Score          7.735      1.817   4.256 0.000275 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 60.51 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
## Residual analysis
plot(m3)

## ANOVA for m1 and m3
anova(m1, m3)
## Analysis of Variance Table
## 
## Model 1: Price_. ~ Megapixels
## Model 2: Price_. ~ Megapixels + Weight_oz + Score
##   Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
## 1     26 181525                                  
## 2     24  87881  2     93644 12.787 0.0001658 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Adjusted R-squared for m1 and m3
summary(m1)$adj.r.squared
## [1] -0.01842446
summary(m3)$adj.r.squared
## [1] 0.4658671
## ANOVA for m2 and m3
anova(m2, m3)
## Analysis of Variance Table
## 
## Model 1: Price_. ~ Megapixels + Weight_oz
## Model 2: Price_. ~ Megapixels + Weight_oz + Score
##   Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
## 1     25 154217                                  
## 2     24  87881  1     66336 18.116 0.0002752 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Adjusted R-squared for m2 and m3
summary(m2)$adj.r.squared
## [1] 0.1001771
summary(m3)$adj.r.squared
## [1] 0.4658671
## Add the Nikon indicator variable
cameradataset$Nikon <- ifelse(cameradataset$Brand == 0, 1, 0)

## Fit a regression model m4
m4 <- lm(Price_. ~ Megapixels + Weight_oz + Score + Nikon, data = cameradataset)

# Summary of the model
summary(m4)
## 
## Call:
## lm(formula = Price_. ~ Megapixels + Weight_oz + Score + Nikon, 
##     data = cameradataset)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -76.22 -34.98 -14.31  36.88 174.16 
## 
## Coefficients: (1 not defined because of singularities)
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -499.381    146.142  -3.417 0.002261 ** 
## Megapixels     8.318      6.466   1.286 0.210573    
## Weight_oz     17.418     12.627   1.379 0.180467    
## Score          7.735      1.817   4.256 0.000275 ***
## Nikon             NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 60.51 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
## Residual analysis
plot(m4)

## Model for Nikon
m4_nikon <- lm(Price_. ~ Megapixels + Weight_oz + Score, data = subset(cameradataset, Brand.1 == 0))

## Model for Canon
m4_canon <- lm(Price_. ~ Megapixels + Weight_oz + Score, data = subset(cameradataset, Brand.1 == 1))


## Plot each model on the data graph
predictions <- predict(m4_nikon)

names(cameradataset)
## [1] "Observation" "Brand"       "Price_."     "Megapixels"  "Weight_oz"  
## [6] "Score"       "Brand.1"     "Nikon"