This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.
library(readxl)
data <- read_excel("problem3-oxygenpurity.xls")
modellr <- lm(purity ~ hydro, data = data)
coefficients <- coef(modellr)
print(coefficients)
(Intercept) hydro
78.90306 10.71324
#Answer 1
#The coefficient of beta0=78.90306 and beta1=10.71324 after fitting the simple linear regression model.
#Answer 2
summary(modellr)
Call:
lm(formula = purity ~ hydro, data = data)
Residuals:
Min 1Q Median 3Q Max
-4.5048 -3.5605 0.0294 2.6550 7.3408
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 78.903 3.843 20.532 2.73e-16 ***
hydro 10.713 3.204 3.344 0.00281 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.494 on 23 degrees of freedom
Multiple R-squared: 0.3272, Adjusted R-squared: 0.2979
F-statistic: 11.18 on 1 and 23 DF, p-value: 0.002814
#From statistics,we can see that it rejects Null hypothesis because it is a significant linear regression.
#Answer 3
#The coefficient of determination value is r^2 is 0.3272.This value indicates that there is 32.2% of the variation in the dependent variable is explained by the independent variable.
#Answer 4
# Calculating the 95% confidence interval for a specific coefficient ( beta1)
ci <- confint(modellr, "hydro", level = 0.95)
ci
2.5 % 97.5 %
hydro 4.08618 17.34031
#Answer4
ci1 <- confint(modellr, level = 0.95)
ci1
2.5 % 97.5 %
(Intercept) 70.95322 86.85289
hydro 4.08618 17.34031
#The confidence interval on slope Beta 0 (Lower bound is 70.95322 and upper bound is 86.85289).
#The confidence interval on slope Beta 1 (Lower bound is 4.08618 and upper bound is 17.34031).
#Answer 5
# For finding the mean purity , we are defining the value of hydrocarbon percentage
valuehydro<-1.05
data2<-data.frame(hydro=valuehydro)
predic<-predict(modellr,newdata=data2,interval = "confidence",level=0.95)
cat("predicted mean purity:",predic[1],"\n")
predicted mean purity: 90.15196
cat("Lower bound:",predic[2],"\n")
Lower bound: 88.47041
cat("upper bound:",predic[3],"\n")
upper bound: 91.83351
#Answer 6
#correlation coefficient r between Y and X
correlation<- cor(data$purity,data$hydro)
cat("The correlation coefficient:",correlation,"\n")
The correlation coefficient: 0.5719793
#Answer 7
#Test the hypothesis:
no<-length(data$purity)
tstats<-correlation*sqrt((no-2)/1-correlation^2)
degrfre<-no-2
# Perform a two-sample t-test
t_test_result <- t.test(data)
# Get the two-tailed p-value
pvalue <- t_test_result$p.value
#significant level
a<-0.05
#comparing p value to a
if(pvalue<a) {
cat("It reject the null hypothesis,\n")
cat("There is a significant correlation between purity and hydrocarbons,\n")
} else {
cat("It fails to reject the null hypothesis,\n")
cat("There is no significant correlation between purity and hydrocarbons,\n")
}
It reject the null hypothesis,
There is a significant correlation between purity and hydrocarbons,
#print t stats
cat("t statistics",tstats)
t statistics 2.723537
#print p value
cat("pvalue",pvalue)
pvalue 3.64393e-09