Parag Gupta

John E. Kolassa

01:960:463

Homework-1

Packages

library(MPV)
## Loading required package: lattice
## Loading required package: KernSmooth
## KernSmooth 2.23 loaded
## Copyright M. P. Wand 1997-2009
## Loading required package: randomForest
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:randomForest':
## 
##     margin
library(readxl)

1.a. Fit a simple linear regression model relating total heat flux y (kilowatts) to the radial deflection of the deflected rays x4 (milliradians).

hfdata <- table.b2
first <- lm(y ~ x4,data=hfdata)
first
## 
## Call:
## lm(formula = y ~ x4, data = hfdata)
## 
## Coefficients:
## (Intercept)           x4  
##       607.1        -21.4

The above analysis shows that the total heat flux y (kilowats) and the radial deflection of the deflected rays x4, are related by the formula:
y = -21.4(x4) + 607.1

β0 = 607.1

β1 = -21.4

ggplot(data = hfdata, mapping = aes(y = y, x = x4)) + geom_point() +
  geom_abline(slope = -21.4, intercept = 607.1, col="blue")

1.b. Construct the analysis-of-variance table and test for significance of regression

ANOVA Table

sec <- aov(y ~ x4, hfdata)
sumSec <- summary(sec)
sumSec
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## x4           1  10579   10579   69.61 5.94e-09 ***
## Residuals   27   4103     152                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Analysis of Regression

  • The null hypothesis (H0) for the overall significance test is that all the regression coefficients are zero (i.e., the model has no predictive power).

  • The alternative hypothesis (H1) is that at least one of the regression coefficients is non-zero.

sumSec[[1]]$`Pr(>F)`[1]
## [1] 5.935009e-09

Given the p-value is less than 0.05, we fail to accept the the null Hypothesis(H0). thus, we can’t say the model has no predictive power. Hence the Model is Significant

1.c. Find a 99% CI on the slope.

confint(first,'x4', level = 0.99)
##        0.5 %    99.5 %
## x4 -28.50995 -14.29497

We can say with 99% Confidence that the Value of the slope lies within the interval (-28.50995,-14.29497).

1.d. Calculate R^2.

firstSum <- summary(first)
firstSum$r.squared
## [1] 0.7205242

The value for the coefficient of determination (R^2), indicates that the model has a good fit as the lot of the variability of the dependant variable can be explained through the model.

1.e. Find a 95% CI on the mean heat flux when the radial deflection is 16.5 milliradians.

predict.lm(first, newdata=data.frame(x4=16.5), interval="confidence")
##        fit      lwr      upr
## 1 253.9627 249.1468 258.7787

We can with 95% certainty that the true mean heat flux when radial deflection is 16.5 milliradians lies between 249.1468 and 258.7787.


propData <- table.b4

2.a. Fit a simple linear regression model relating selling price of the house to the current taxes ( x1 ).

prop <- lm(y~x1,data=propData)
prop
## 
## Call:
## lm(formula = y ~ x1, data = propData)
## 
## Coefficients:
## (Intercept)           x1  
##      13.320        3.324

β0 = 13.20

β1 = 3.324

y = 3.324(x1) + 13.20 [y = Sale Price of House/1000 & x1 = Total Taxes/1000]

ggplot(data = propData, mapping = aes(y = y, x = x1)) + geom_point() +
geom_abline(slope = 3.324, intercept = 13.320, col="blue")

2.b. Test for significance of regression.

  • The null hypothesis (H0) for the overall significance test is that all the regression coefficients are zero (i.e., the model has no predictive power).

  • The alternative hypothesis (H1) is that at least one of the regression coefficients is non-zero.

propAn <- aov(y~x1,data=propData)
propSum <- summary(propAn)
propSum[[1]]$`Pr(>F)`[1]
## [1] 2.051257e-08

Given that the P-Value of the model = 2.051257e-08, which is less than 0.05, we fail to accept the Null Hypothesis, thus, we can’t say the model has no predictive power. Hence the Model is Significant

2.c. What percent of the total variability in selling price is explained by this model?

The variability in selling price that can be explained by this model can be seen as the value of the coefficient of determination (R^2)

cod <- summary(prop)$r.squared
cod*100
## [1] 76.73344

From the above output we can see that around 76.73% of the total variability in selling price can be explained by this model.

2.d. Find a 95% CI on β1 .

confint(prop,'x1',level=0.95)
##       2.5 %   97.5 %
## x1 2.514988 4.133754

We can say with 95% Confidence that the Value of β1 lies within the interval (2.514988,4.133754).

2.e. Find a 95% CI on the mean selling price of a house for which the current taxes are $750.

predict.lm(prop, newdata=data.frame(x1=750/1000), interval="confidence")
##        fit      lwr      upr
## 1 15.81346 11.06792 20.55899

We can say with 95% certainty that the true mean price of the house for which the current taxes are $750 lies within the range (11.06792, 20.55899).


purData <- read_excel("Book1.xlsx")

3.a. Fit a simple linear regression model to the data.

purlm <- lm(y~x,data=purData)
purlm
## 
## Call:
## lm(formula = y ~ x, data = purData)
## 
## Coefficients:
## (Intercept)            x  
##       77.89        11.78

β0 = 77.89

β1 = 11.78

y = 11.78(x) + 77.89 [y = Purity(%), x = Hydrocarbon(%)]

ggplot(data = purData, mapping = aes(y = y, x = x)) + geom_point() +
geom_abline(slope = 11.78, intercept = 77.89, col="blue")

3.b. Test the hypothesis H0 : β1 = 0 .

H0 : β1 = 0

H1 : β1 ≠ 0

purAn <- aov(y~x,data=purData)
purSum <- summary(purAn)
purSum[[1]]$`Pr(>F)`[1]
## [1] 0.003296166

Given that the P-Value of the model, is less than 0.05, we fail to accept the Null Hypothesis, thus, we can’t say that H0 : β1 = 0 . Hence the Model is Significant

3.c. Calculate R^2

summary(prop)$r.squared
## [1] 0.7673344

R^2 = 0.7673344

3.d. Find a 95% CI on the slope.

confint(purlm,'x',level=0.95)
##      2.5 %   97.5 %
## x 4.469286 19.08911

We can say with 95% Confidence that the Value of the slope lies within the interval (4.469286,19.08911).

3.e. Find a 95% CI on the mean purity when the hydrocarbon percentage is 1.00.

predict(purlm, newdata = data.frame(x=1.00),interval = "confidence")
##       fit     lwr      upr
## 1 89.6733 87.5227 91.82389

We can say with 95% confidence that the mean purity when the hydrocarbon percentage is 1.00 lies within (87.51017, 91.81845).


4.a. What is the correlation between oxygen purity and hydrocarbon percentage?

cor(purData$y,purData$x)*100
## [1] 62.37189

There is an approximately 62.37% correlation between Oxygen Purity (%) and Hydrocarbon (%)

4.b. Test the hypothesis that ρ = 0.

Ho : ρ = 0

H1 : ρ ≠ 0

l <- cor.test(purData$y,purData$x)
l
## 
##  Pearson's product-moment correlation
## 
## data:  purData$y and purData$x
## t = 3.3854, df = 18, p-value = 0.003296
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.2502766 0.8356054
## sample estimates:
##       cor 
## 0.6237189

Thus, from the above we see that the p-value = 0.003296, which is less than 0.05. Hence we cannot accept the Null Hypothesis, and favor the Alternate over the Null Hypothesis.

4.c. Construct a 95% CI for ρ

l$conf.int
## [1] 0.2502766 0.8356054
## attr(,"conf.level")
## [1] 0.95

We can say with 95% confidence that the value of ρ lies within the interval (0.2502766,0.8356054)