I need help plotting the question below in R
We solve this using the open source statistical package R , the R snippet is as below
# read the data into R dataframe
df<- read.csv("VIX.csv",header=TRUE)
str(df)
fit <- lm(cost~VIX,data=df)
summary(fit)
newdata = data.frame(VIX=15)
predict(fit, newdata, interval="predict")
# 1. Add predictions
pred.int <- predict(fit, interval = "prediction")
mydata <- cbind(df, pred.int)
# 2. Regression line + confidence intervals
library("ggplot2")
p <- ggplot(mydata, aes(VIX, SPX)) + geom_point() +
stat_smooth(method = lm)
# 3. Add prediction intervals
p + geom_line(aes(y = lwr), color = "red", linetype = "dashed")+
geom_line(aes(y = upr), color = "red", linetype = "dashed")
The results are
> summary(fit)
Call:
lm(formula = SPX ~ VIX, data = df)
Residuals:
Min 1Q Median 3Q Max
-284.70 -88.06 -13.35 82.68 223.65
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 270.23 58.37 4.629 0.000208 ***
VIX 74.56 11.54 6.460 4.46e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 149.2 on 35 degrees of freedom
Multiple R-squared: 0.6986, Adjusted R-squared: 0.6819
F-statistic: 41.73 on 1 and 35 DF, p-value: 4.459e-06
> predict(fit, newdata, interval="predict")
fit lwr upr
1 1388.613 973.4366 1803.79
I need help plotting the question below in R Plot a summary of the predictive distribution...