#Essentials to Business Analytics
# Question 1
x<-c(17.9,16.2,15.0,16.0,17.3,13.2,16.3,17.2,17.7,14.2)
y<-c(2200,6350,8470,6300,4100,8700,6100,2680,3500,8100)
plot(x,y,col="blue",lwd=5,main="Bicycle World Weight and Price",xlab="Weight",ylab = "Price")
abline(lm(y ~ x))

model3<-lm(y ~ x)
model3
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##       28818        -1439
summary(model3)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1387.1  -715.9   164.6   679.9  1237.1 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  28818.0     3267.3   8.820 2.15e-05 ***
## x            -1439.0      202.1  -7.121 9.99e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 942.3 on 8 degrees of freedom
## Multiple R-squared:  0.8637, Adjusted R-squared:  0.8467 
## F-statistic:  50.7 on 1 and 8 DF,  p-value: 9.994e-05
data<-data.frame(x,y,xdev=(x-mean(x)),ydev=(y-mean(y)),xdevydev=((x-mean(x))*(y-mean(y))),xdev2=(x-mean(x))^2,ydev2=(y-mean(y))^2)
data
##       x    y xdev  ydev xdevydev xdev2    ydev2
## 1  17.9 2200  1.8 -3450    -6210  3.24 11902500
## 2  16.2 6350  0.1   700       70  0.01   490000
## 3  15.0 8470 -1.1  2820    -3102  1.21  7952400
## 4  16.0 6300 -0.1   650      -65  0.01   422500
## 5  17.3 4100  1.2 -1550    -1860  1.44  2402500
## 6  13.2 8700 -2.9  3050    -8845  8.41  9302500
## 7  16.3 6100  0.2   450       90  0.04   202500
## 8  17.2 2680  1.1 -2970    -3267  1.21  8820900
## 9  17.7 3500  1.6 -2150    -3440  2.56  4622500
## 10 14.2 8100 -1.9  2450    -4655  3.61  6002500
SSy <- sum(data$ydev2)
SSy^2
## [1] 2.716578e+15
# Graph indidcates a negative linear relationship between Weight and Price
# Estimated Regression Model would be y = b0+B1*x --> y=-1439(x)+28818

# Question 2
x<-c(20,20,40,30,60,40)
y<-c(21,19,15,16,14,17)
plot(x,y, col="green",lwd=5,main="# of defective parts to Line Speed",xlab="Line Speed",ylab="Defective Parts")
abline(lm(y~ x))    
model4<-lm(y ~ x)
model4
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##     22.1739      -0.1478
xmean<-mean(x)
ymean<-mean(y)
points(xmean,ymean,col="Red",lwd=10)

summary(model4)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##       1       2       3       4       5       6 
##  1.7826 -0.2174 -1.2609 -1.7391  0.6957  0.7391 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 22.17391    1.65275  13.416 0.000179 ***
## x           -0.14783    0.04391  -3.367 0.028135 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.489 on 4 degrees of freedom
## Multiple R-squared:  0.7391, Adjusted R-squared:  0.6739 
## F-statistic: 11.33 on 1 and 4 DF,  p-value: 0.02813
SSE<-c(1.78,.217,1.739,.695,.7391)^2
sum(SSE)
## [1] 7.268904
#Essentials to Business Analytics
# Question 3
x<-c(13,10,20,28,32,17,24,31,40,38)
y<-c(17,22,30,37,47,30.5,32.4,39,51.5,40)
plot(x,y,col="purple",lwd=5,main="Annual Maintenance Expense - Weekly usage",xlab = "Weekly usage hours",ylab="Annual Maintenance (100s)")
abline(lm(y~ x))
xmean<-mean(x)
ymean<-mean(y)
points(xmean,ymean,col="red", lwd=10)

model5<-lm(lm(y~ x)) 
summary(model5)
## 
## Call:
## lm(formula = lm(y ~ x))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.7504 -1.0566  0.0997  2.6192  5.9710 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  10.5145     3.7473   2.806 0.022990 *  
## x             0.9536     0.1383   6.897 0.000125 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.252 on 8 degrees of freedom
## Multiple R-squared:  0.856,  Adjusted R-squared:  0.8381 
## F-statistic: 47.57 on 1 and 8 DF,  p-value: 0.0001249
w<-10.5145+x*.95
SSE<-(y-w)^2
sum(SSE)
## [1] 144.7592
# Question 8
x<-c(22,29,36,47,63,77,73,87,92,101,110,28,59,68,68,91,42,65,110)
y<-c(16.2,16,13.8,11.5,12.5,12.9,11.2,13,11.8,10.8,8.3,12.5,11.1,15,12.2,13,15.6,12.7,8.3)
plot(x,y,col="orange",lwd=5,main="Miles to Price Comparrison", ylab = "Price(1000s)",xlab = "Miles(1000s)")

#There is a negitive linear relationship between Price and Miles
lm(y~x)
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##    16.46976     -0.05877
# Estimated Regression Equation - y= -.05877(x) + 16.46976
A<-(-.05877*x)+16.46976
t.test(A)
## 
##  One Sample t-test
## 
## data:  A
## t = 33.793, df = 18, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  11.76754 13.32773
## sample estimates:
## mean of x 
##  12.54764
t.test(y)
## 
##  One Sample t-test
## 
## data:  y
## t = 24.8, df = 18, p-value = 2.284e-15
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  11.48440 13.61033
## sample estimates:
## mean of x 
##  12.54737