Hobson GLM - Exercise 6.1
Purpose
To work out exercises from Chap 6 '' Exercise 6.1
> folder <- "C:/Cauldron/garage/R/soulcraft/Volatility/Learn/Dobson-GLM/" > file.input <- paste(folder, "Table 6.15-Sugar consumption.csv", + sep = "") > data <- read.csv(file.input, header = T, stringsAsFactors = F) > data$decade <- 1:6 |
Plot sugar consumption against time separately for refined sugar and
sugar in manufactured foods. Fit simple linear regression models to summarize the pattern ofconsumption ofeac h form ofsugar. Calculate 95% confidence intervals for the average annual change in consumption for each form.
> par(mfrow = c(1, 1))
> xlim <- range(data$decade)
> ylim <- c(0, 50)
> xlab <- "Decade"
> ylab <- "Consumption"
> main <- "Trends"
> plot(data$decade, data$refined, type = "l", col = "blue", lwd = 2,
+ xlab = xlab, ylab = ylab, xlim = xlim, ylim = ylim, main = main)
> par(new = T)
> plot(data$decade, data$manufactured, type = "l", col = "red",
+ lwd = 2, xlab = "", ylab = "", xlim = xlim, ylim = ylim,
+ main = "")
> legend("topleft", legend = c("refined", "manufactured"), fill = c("blue",
+ "red")) |

> fit1 <- lm(refined ~ decade, data)
> plot(data$decade, data$refined, type = "l", col = "blue", lwd = 2,
+ xlab = xlab, ylab = ylab, xlim = xlim, ylim = ylim, main = main)
> par(new = T)
> plot(data$decade, fitted(fit1), type = "l", col = "blue", lty = "dashed",
+ lwd = 2, xlab = xlab, ylab = ylab, xlim = xlim, ylim = ylim,
+ main = main)
> par(new = T)
> plot(data$decade, predict(fit1, int = "c")[, 2], type = "l",
+ col = "blue", lty = "dotted", lwd = 2, xlab = xlab, ylab = ylab,
+ xlim = xlim, ylim = ylim, main = main)
> par(new = T)
> plot(data$decade, predict(fit1, int = "c")[, 3], type = "l",
+ col = "blue", lty = "dotted", lwd = 2, xlab = xlab, ylab = ylab,
+ xlim = xlim, ylim = ylim, main = main)
> par(new = T)
> fit2 <- lm(manufactured ~ decade, data)
> plot(data$decade, data$manufactured, type = "l", col = "red",
+ lwd = 2, xlab = "", ylab = "", xlim = xlim, ylim = ylim,
+ main = "")
> par(new = T)
> plot(data$decade, fitted(fit2), type = "l", lty = "dashed", col = "red",
+ lwd = 2, xlab = "", ylab = "", xlim = xlim, ylim = ylim,
+ main = "")
> par(new = T)
> plot(data$decade, predict(fit2, int = "c")[, 2], type = "l",
+ lty = "dotted", col = "red", lwd = 2, xlab = "", ylab = "",
+ xlim = xlim, ylim = ylim, main = "")
> par(new = T)
> plot(data$decade, predict(fit2, int = "c")[, 3], type = "l",
+ lty = "dotted", col = "red", lwd = 2, xlab = "", ylab = "",
+ xlim = xlim, ylim = ylim, main = "")
> legend("topleft", legend = c("refined", "manufactured"), fill = c("blue",
+ "red"))
> legend("topright", legend = c("rawdata", "fit", "95%bands"),
+ lty = c("solid", "dashed", "dotted")) |

> confint(fit1)
2.5 % 97.5 %
(Intercept) 34.300201 44.846466
decade -6.236872 -3.528842
> confint(fit2)
2.5 % 97.5 %
(Intercept) 8.568622 19.178045
decade 2.255019 4.979267 |
Annual Percapita Consumption of refined sugar decreased by -4.88 kg. Consumption of Manufactured Sugar increased by 3.62 kg
Calculate the total average sugar consumption for each period and plot these data against time. Using suitable models test the hypothesis that total sugar consumption did not change over time.
> data$total <- data$refined + data$manufactured > par(mfrow = c(1, 1)) > xlim <- range(data$decade) > ylim <- c(0, 100) > xlab <- "Decade" > ylab <- "Total Consumption" > main <- "Trends" > plot(data$decade, data$total, type = "l", col = "blue", lwd = 2, + xlab = xlab, ylab = ylab, xlim = xlim, ylim = ylim, main = main) |

- Sample average
> t.test(data$total - mean(data$total))
One Sample t-test
data: data$total - mean(data$total)
t = 0, df = 5, p-value = 1
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-3.959262 3.959262
sample estimates:
mean of x
1.184093e-15 |
confidence bands show that the 0 lies with in them
Hence the series has not changed much from the sample average
> fit3 <- lm(data$total ~ data$decade)
> summary(fit3)
Call:
lm(formula = data$total ~ data$decade)
Residuals:
1 2 3 4 5 6
-3.8810 3.3848 0.9505 0.3162 2.3819 -3.1524
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 53.447 3.057 17.483 6.28e-05 ***
data$decade -1.266 0.785 -1.612 0.182
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.284 on 4 degrees of freedom
Multiple R-squared: 0.3939, Adjusted R-squared: 0.2424
F-statistic: 2.6 on 1 and 4 DF, p-value: 0.1822
> confint(fit3)
2.5 % 97.5 %
(Intercept) 44.958991 61.9343423
data$decade -3.445148 0.9137196 |
confidence bands of the estimate show that the 0 lies with in them
Hence the series has not changed much from the sample average