Please do it in R studio( install package\"alr4\" then load data file\"oldfaith\
ID: 2923050 • Letter: P
Question
Please do it in R studio( install package"alr4" then load data file"oldfaith")! Please show code and work. In order to access to "oldfaith", it requires to install package"alr4" first.
Explanation / Answer
Programme: R
install.packages("alr4")
library(alr4)
df <- data.frame(oldfaith)
attach(df)
summary(df)
cor(df)
plot(df)
#Linear regression Interval Vs Duration
lr <- lm(Interval~Duration)
summary(lr)
#Duration =250 for prediciton
xd <- data.frame(Duration= c(250))
#applying prediciton for linear regression
predict(lr,xd, interval= "prediction")
#confidence interval=0.80
predict(lr,xd, interval = 'confidence', level = 0.80)
With output:
> summary(df)
Duration Interval
Min. : 96.0 Min. :43.00
1st Qu.:130.0 1st Qu.:58.00
Median :240.0 Median :76.00
Mean :209.9 Mean :71.11
3rd Qu.:267.8 3rd Qu.:82.00
Max. :306.0 Max. :96.00
> cor(df)
Duration Interval
Duration 1.0000000 0.8960697
Interval 0.8960697 1.0000000
> lr <- lm(Interval~Duration)
> summary(lr)
Call:
lm(formula = Interval ~ Duration)
Residuals:
Min 1Q Median 3Q Max
-12.3337 -4.5250 0.0612 3.7683 16.9722
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 33.987808 1.181217 28.77 <2e-16 ***
Duration 0.176863 0.005352 33.05 <2e-16 ***
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 6.004 on 268 degrees of freedom
Multiple R-squared: 0.8029, Adjusted R-squared: 0.8022
F-statistic: 1092 on 1 and 268 DF, p-value: < 2.2e-16
> xd <- data.frame(Duration= c(250))
> predict(lr,xd, interval= "prediction")
fit lwr upr
1 78.20354 66.35401 90.05307
> predict(lr,xd, interval = 'confidence', level = 0.80)
fit lwr upr
1 78.20354 77.65908 78.748