Dhruv Garg

2024-06-15

Question 1

A.The data is made read into the program

sales<- read.csv("sales.csv", header= TRUE)

Creating the Scatter-Plot between Sales and Index

# Create scatter plot of variable 1 against variable 2
plot(sales$Sales,sales$Index , xlab = "Sales", ylab = "Index ", main = "Scatter Plot")

“The relationship between sales and the index variable appears to be uneven and non-linear. The index variable exhibits fluctuations, initially dipping into negative values, then rising to zero, and finally increasing abruptly to 10. This erratic behavior suggests that the relationship between sales and the index is not characterized by a consistent or predictable pattern.

B.Simple linear regression model

M1 <- lm(Sales ~ Index, data = sales)
par(mfrow = c(2, 2))  
plot(M1, which = 1)
plot(M1, which = 2)
plot(M1, which = 3)
plot(M1, which = 5)

par(mfrow = c(1, 1))
# Brief summary of the model
summary(M1)
## 
## Call:
## lm(formula = Sales ~ Index, data = sales)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -11.139  -4.988  -1.086   4.028  17.152 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -17.7007     0.8278  -21.38   <2e-16 ***
## Index         3.2464     0.1444   22.48   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.45 on 241 degrees of freedom
## Multiple R-squared:  0.6772, Adjusted R-squared:  0.6759 
## F-statistic: 505.6 on 1 and 241 DF,  p-value: < 2.2e-16

According to the linear regression model, sales are predicted to rise by around 3.25 units for every unit increase in the Index. With an adjusted R-squared of 0.676, the model has significant explanatory power; almost 67.6% of the variability in Sales can be accounted for by the Index variable.

c. Fitting 2 polynomial models of order 2 and order 3.

M1 <- lm(Sales ~ Index, data = sales)

#(order 2)
M2 <- lm(Sales ~ poly(Index, 2), data = sales)

#(order 3)
M3 <- lm(Sales ~ poly(Index, 3), data = sales)

#Comparing
summary(M1)
## 
## Call:
## lm(formula = Sales ~ Index, data = sales)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -11.139  -4.988  -1.086   4.028  17.152 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -17.7007     0.8278  -21.38   <2e-16 ***
## Index         3.2464     0.1444   22.48   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.45 on 241 degrees of freedom
## Multiple R-squared:  0.6772, Adjusted R-squared:  0.6759 
## F-statistic: 505.6 on 1 and 241 DF,  p-value: < 2.2e-16
summary(M2)
## 
## Call:
## lm(formula = Sales ~ poly(Index, 2), data = sales)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -5.755 -1.967  0.037  1.749  7.827 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      -1.5799     0.1611  -9.807   <2e-16 ***
## poly(Index, 2)1 145.0378     2.5112  57.757   <2e-16 ***
## poly(Index, 2)2  92.2701     2.5112  36.744   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.511 on 240 degrees of freedom
## Multiple R-squared:  0.9513, Adjusted R-squared:  0.9509 
## F-statistic:  2343 on 2 and 240 DF,  p-value: < 2.2e-16
summary(M3)
## 
## Call:
## lm(formula = Sales ~ poly(Index, 3), data = sales)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.7850 -1.9384  0.0545  1.7424  7.8321 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      -1.5799     0.1614  -9.788   <2e-16 ***
## poly(Index, 3)1 145.0378     2.5162  57.641   <2e-16 ***
## poly(Index, 3)2  92.2701     2.5162  36.670   <2e-16 ***
## poly(Index, 3)3  -0.4874     2.5162  -0.194    0.847    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.516 on 239 degrees of freedom
## Multiple R-squared:  0.9513, Adjusted R-squared:  0.9507 
## F-statistic:  1556 on 3 and 239 DF,  p-value: < 2.2e-16

According to the linear model, there is a fundamental positive association and a rise in Sales of about 3.25 units for every unit increase in the Index. In the meanwhile, the cubic and quadratic models offer better fits, accounting for non-linearities in the connection and therefore improving the models’ capacity to account for changes in sales.

D-Plotting the data and adding three predicted lines from model M1,M2 and M3.

plot(sales$Index, sales$Sales, xlab="Index", ylab="Sales",
main="Sales vs. Index with predicted lines")

lines(sales$Index, predict(M1), col = "blue")  
lines(sales$Index, predict(M2), col = "green")   
lines(sales$Index, predict(M3), col = "black")

e.Assessing the significance of the linear,quadratic and cubic terms

anova_M3 <- anova(M3)
print(anova_M3)
## Analysis of Variance Table
## 
## Response: Sales
##                 Df  Sum Sq Mean Sq F value    Pr(>F)    
## poly(Index, 3)   3 29550.0  9850.0  1555.8 < 2.2e-16 ***
## Residuals      239  1513.2     6.3                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

With sales regressed on the Index, the analysis of variance (ANOVA) table shows a substantial overall fit for the cubic polynomial regression model, supported by a very significant F-value (F = 1555.8, p < 2.2e-16). This indicates that a significant portion of the variation in sales, over and above what would be predicted by chance alone, is explained by the cubic model.

F.Choosing the best model among M1,M2 and M3

Among the models above according to the information above M2 would be the best model among all of them.The F-statistic in M2 is substantially higher than M1,M2 which indicates that this is the best among overall fit.Moreover,M2 has the smallest residual standard error, suggesting that it provides the best fit to the data among the three models.

Question 2

-Making the file read into the program.

campaign<- read.csv("campaign.csv", header= TRUE)

b-Full interaction model

Yijk = µ + αi + βj + γij + εijk, Yijk: Score αi: Region Effect - 3 Levels βj: Region Effect - 6 Levels γij: Interaction Effect Between Score and Region εijk ∼ N(0,σ2)

C.Analysing the data to study the effect of type and region with engagement score of 5 percent.

campaign.lm <- lm(Score ~ Type * Region, data = campaign)
par(mfrow = c(1, 2))
plot(campaign.lm, which = c(1, 2))

The points in residual vs fitted chart are scattered in a uniform way,whereas in the q-q residual chart follows a positive linear relationship.Now that we have assessed the graphs we can assume they are true and we can begin the hypotheses test.

The null hypotheses H0:YIJ=0 for i,j

Alternate hypotheses H1: atleast one YIJ≠0

anova(campaign.lm)
## Analysis of Variance Table
## 
## Response: Score
##             Df  Sum Sq Mean Sq  F value    Pr(>F)    
## Type         2 1585.09  792.54 330.5242 < 2.2e-16 ***
## Region       1  325.45  325.45 135.7281 2.336e-16 ***
## Type:Region  2    1.29    0.64   0.2683    0.7657    
## Residuals   54  129.48    2.40                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1


With a p-value of 0.7657, the model should be revised as the current variable may lack statistical significance, adhering to the constraint of modifying only one variable.

D.Repeating the test analysis for observing main effects

campaign <-aov(Score ~ Type*Region,data=campaign)
result <- summary(campaign)
print(campaign)
## Call:
##    aov(formula = Score ~ Type * Region, data = campaign)
## 
## Terms:
##                      Type    Region Type:Region Residuals
## Sum of Squares  1585.0896  325.4545      1.2865  129.4835
## Deg. of Freedom         2         1           2        54
## 
## Residual standard error: 1.548497
## Estimated effects may be unbalanced