R markdown Assignment

Phase 1

Loading data

data <- read.csv("C:/Users/Dell/Desktop/SI/camdata.csv")
attach(data)
summary(data)
##   Observation       Brand              Price_.         Megapixels   
##  Min.   : 1.00   Length:28          Min.   : 20.00   Min.   :10.00  
##  1st Qu.: 7.75   Class :character   1st Qu.: 28.00   1st Qu.:12.00  
##  Median :14.50   Mode  :character   Median : 40.50   Median :12.00  
##  Mean   :14.50                      Mean   : 44.07   Mean   :12.86  
##  3rd Qu.:21.25                      3rd Qu.: 50.00   3rd Qu.:14.00  
##  Max.   :28.00                      Max.   :100.00   Max.   :16.00  
##    Weight_oz         Score          Brand.1      
##  Min.   :4.000   Min.   :14.00   Min.   :0.0000  
##  1st Qu.:5.000   1st Qu.:15.00   1st Qu.:0.0000  
##  Median :6.000   Median :16.00   Median :0.0000  
##  Mean   :5.821   Mean   :15.82   Mean   :0.4643  
##  3rd Qu.:7.000   3rd Qu.:17.00   3rd Qu.:1.0000  
##  Max.   :7.000   Max.   :17.00   Max.   :1.0000
str(data)
## '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  83 50 75 50 45 50 50 33 33 28 ...
##  $ 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  17 15 17 16 15 17 15 17 15 15 ...
##  $ Brand.1    : int  1 1 1 1 1 1 1 1 1 1 ...

Part D1

Test the hypothesis that the Average Price of Nikon and Canon are the Same

qqnorm(Price_., main="QQ Plot",xlab="z score", ylab="prices", col="green")

qqline(Price_., col="red")

hist(Price_., main="Histogram", xlab="Price", ylab="Frequency",col="purple")

after we have done the transformation of data we find out that the normality of data has been affected but it is still aproximately normal.

Equal Standard Deviations

Nikonvar <- var(Price_.[Brand=="Nikon"])
canonvar <- var(Price_.[Brand=="Canon"])
Nikonvar
## [1] 510.5429
# Hypothesis
t.test(Price_.~ Brand,mu = 0, alt = "two.sided",conf=0.95, var.eq =T, paired=F)
## 
##  Two Sample t-test
## 
## data:  Price_. by Brand
## t = -0.088746, df = 26, p-value = 0.93
## alternative hypothesis: true difference in means between group Canon and group Nikon is not equal to 0
## 95 percent confidence interval:
##  -17.09920  15.68381
## sample estimates:
## mean in group Canon mean in group Nikon 
##            43.69231            44.40000

Part 2

Coding Data on to a new Variable

WeightCategory <- ifelse(Weight_oz<6,"Light",ifelse(Weight_oz==6,"Medium","Heavy"))
camera<- WeightCategory
camera
##  [1] "Heavy"  "Light"  "Heavy"  "Medium" "Light"  "Heavy"  "Light"  "Heavy" 
##  [9] "Light"  "Light"  "Light"  "Medium" "Heavy"  "Light"  "Heavy"  "Medium"
## [17] "Heavy"  "Light"  "Medium" "Light"  "Medium" "Medium" "Medium" "Heavy" 
## [25] "Heavy"  "Light"  "Light"  "Light"

ONE WAY ANNOVA

Testing Assumptions

boxplot(Price_.~WeightCategory,horizontal = T ,col=c(5,3,10))

The plot shows that data is not normal but the sample size is large we assume data is normal.

Equal Population SD’s (Homosedasticity)

Lightvar <- var(Price_.[WeightCategory=="Light"])
Mediumvar <- var(Price_.[WeightCategory=="Medium"])
Heavyvar <- var(Price_.[WeightCategory=="Heavy"])
Lightvar                
## [1] 176.6288
SDcheckweight <- Heavyvar/Mediumvar
SDcheckweight
## [1] 7.566438

Since we have already modified the data, it would not satisfy the Annova assumptions, but we anticipate that pre-transformed data will.

Hypothesis

H0: AvgPriceLW = AvgPriceMW = AvgPriceHW H1: Average prices of three categories are not equal

ANOVA <-  aov(Price_. ~ WeightCategory)
ANOVA
## Call:
##    aov(formula = Price_. ~ WeightCategory)
## 
## Terms:
##                 WeightCategory Residuals
## Sum of Squares        1261.671 10256.187
## Deg. of Freedom              2        25
## 
## Residual standard error: 20.25457
## Estimated effects may be unbalanced
summary(ANOVA)
##                Df Sum Sq Mean Sq F value Pr(>F)
## WeightCategory  2   1262   630.8   1.538  0.235
## Residuals      25  10256   410.2
"SSTR = 1262/2 = 532"
## [1] "SSTR = 1262/2 = 532"
"SSE = 10256/25 = 410.24 F Statistic = SSTR/SSE = 1.557"
## [1] "SSE = 10256/25 = 410.24 F Statistic = SSTR/SSE = 1.557"
"p value = 0.23"
## [1] "p value = 0.23"
"Alpha = 0.05"
## [1] "Alpha = 0.05"
"p value > Alpha"
## [1] "p value > Alpha"
"Accept H0"
## [1] "Accept H0"
"post hoc analysis if we reject h0"
## [1] "post hoc analysis if we reject h0"

Phase 2

color1 <- "blue"
color2 <- "green"
color3 <- "red"
color4 <- "yellow"
hist(Price_. , col = color1, main = "Price Histogram", xlab = "Price")

hist(Megapixels, col = color2, main = "Megapixel Histogram", xlab = "Megapixels")

hist(Weight_oz, col = color3, main = "Weight Histogram", xlab = "Weight")

hist(Score, col = color4, main = "Score Histogram", xlab = "Score")

Make the matrix scatter plot using Price, Megapixels, Weight, and Score

matrix_colors <- c("blue", "green", "yellow", "red")
pairs(~Price_. + Megapixels + Weight_oz + Score, col=matrix_colors)

m1 -> Fit a regression model to predict the using Price using Megapixels

m1 <- lm(Price_.~Megapixels)
summary(m1)
## 
## Call:
## lm(formula = Price_. ~ Megapixels)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -25.875 -15.008  -5.375   8.070  54.125 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   23.781     28.295   0.840    0.408
## Megapixels     1.578      2.179   0.724    0.475
## 
## Residual standard error: 20.84 on 26 degrees of freedom
## Multiple R-squared:  0.01977,    Adjusted R-squared:  -0.01793 
## F-statistic: 0.5244 on 1 and 26 DF,  p-value: 0.4754

m2 -> Fit a regression model to predict the using Price using Megapixels, and Weight

m2 <- lm(Price_. ~ Megapixels + Weight_oz)
summary(m2)
## 
## Call:
## lm(formula = Price_. ~ Megapixels + Weight_oz)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -36.539 -11.212  -0.285  10.266  43.461 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  -35.177     38.724  -0.908   0.3723  
## Megapixels     2.450      2.091   1.171   0.2525  
## Weight_oz      8.203      3.914   2.096   0.0464 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 19.6 on 25 degrees of freedom
## Multiple R-squared:  0.1663, Adjusted R-squared:  0.09956 
## F-statistic: 2.493 on 2 and 25 DF,  p-value: 0.103

m3 -> Fit a regression model to predict the using Price using Megapixels, Weight and Score

m3 <- lm(Price_. ~ + Weight_oz + Score)
summary(m3)
## 
## Call:
## lm(formula = Price_. ~ +Weight_oz + Score)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -32.665 -10.655  -1.373  11.918  47.335 
## 
## Coefficients: (1 not defined because of singularities)
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)    1.625     22.796   0.071   0.9437  
## Weight_oz      7.291      3.863   1.887   0.0703 .
## Score             NA         NA      NA       NA  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 19.74 on 26 degrees of freedom
## Multiple R-squared:  0.1205, Adjusted R-squared:  0.08668 
## F-statistic: 3.562 on 1 and 26 DF,  p-value: 0.07031

Compare m1, m3 using anova, Adj R^2

anova(m1, m3)
## Analysis of Variance Table
## 
## Model 1: Price_. ~ Megapixels
## Model 2: Price_. ~ +Weight_oz + Score
##   Res.Df   RSS Df Sum of Sq F Pr(>F)
## 1     26 11290                      
## 2     26 10130  0    1160.3

Compare m2, m3 using anova, Adj R^2

anova(m2, m3)
## Analysis of Variance Table
## 
## Model 1: Price_. ~ Megapixels + Weight_oz
## Model 2: Price_. ~ +Weight_oz + Score
##   Res.Df     RSS Df Sum of Sq      F Pr(>F)
## 1     25  9602.9                           
## 2     26 10129.9 -1   -526.97 1.3719 0.2525

Add an indicator variable named Nikon which is 1 if the Brand_2 = 0 otherwise 0

data$Nikon <- ifelse(data$Brand.1 == 0, 1, 0)
data
##    Observation Brand Price_. Megapixels Weight_oz Score Brand.1 Nikon
## 1            1 Canon      83         10         7    17       1     0
## 2            2 Canon      50         12         5    15       1     0
## 3            3 Canon      75         12         7    17       1     0
## 4            4 Canon      50         10         6    16       1     0
## 5            5 Canon      45         12         5    15       1     0
## 6            6 Canon      50         12         7    17       1     0
## 7            7 Canon      50         14         5    15       1     0
## 8            8 Canon      33         10         7    17       1     0
## 9            9 Canon      33         12         5    15       1     0
## 10          10 Canon      28         16         5    15       1     0
## 11          11 Canon      23         14         5    15       1     0
## 12          12 Canon      25         10         6    16       1     0
## 13          13 Canon      23         12         7    17       1     0
## 14          14 Nikon      68         16         5    15       0     1
## 15          15 Nikon      75         16         7    17       0     1
## 16          16 Nikon      50         14         6    16       0     1
## 17          17 Nikon     100         14         7    17       0     1
## 18          18 Nikon      30         14         5    15       0     1
## 19          19 Nikon      43         16         6    16       0     1
## 20          20 Nikon      38         12         5    15       0     1
## 21          21 Nikon      58         14         6    16       0     1
## 22          22 Nikon      45         12         6    16       0     1
## 23          23 Nikon      33         12         6    16       0     1
## 24          24 Nikon      20         12         7    17       0     1
## 25          25 Nikon      20         14         7    17       0     1
## 26          26 Nikon      25         12         4    14       0     1
## 27          27 Nikon      28         12         5    15       0     1
## 28          28 Nikon      33         14         4    14       0     1

m4 -> Fit a regression model using best of the m1, m2, and m3 and include the indicator Nikon

m4 <- lm(data$Price_. ~ data$Megapixels + data$Weight_oz + data$Score + data$Nikon)

summary(m4)
## 
## Call:
## lm(formula = data$Price_. ~ data$Megapixels + data$Weight_oz + 
##     data$Score + data$Nikon)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -35.856 -11.028   0.418  10.521  44.144 
## 
## Coefficients: (1 not defined because of singularities)
##                 Estimate Std. Error t value Pr(>|t|)  
## (Intercept)      -37.217     40.352  -0.922   0.3655  
## data$Megapixels    2.699      2.365   1.141   0.2650  
## data$Weight_oz     8.192      3.990   2.053   0.0511 .
## data$Score            NA         NA      NA       NA  
## data$Nikon        -2.057      8.438  -0.244   0.8095  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 19.98 on 24 degrees of freedom
## Multiple R-squared:  0.1683, Adjusted R-squared:  0.06436 
## F-statistic: 1.619 on 3 and 24 DF,  p-value: 0.2112

write down the model m4 for Nikon and Canon separately and plot each model on the data graph

plot(data$Price_. ~ data$Megapixels, col = ifelse(data$Nikon ==1, "blue", "green"))

plot(data$Price_. ~ data$Weight_oz, col = ifelse(data$Nikon == 1, "red", "green"))

plot(data$Price_. ~ data$Score, col = ifelse(data$Nikon ==1, "blue", "yellow" ))