Adrian G. Barnett

2026-07-11

An Introduction to Generalized Linear Models

install.packages("glmnet")
install.packages("dobson")
library(glmnet)
library(dobson)
data(carbohydrate)
y = carbohydrate$carbohydrate
x = as.matrix(carbohydrate[,c('age', 'weight', 'protein')])
fit = glmnet(x, y)
plot(fit, xvar='lambda')
cvfit = cv.glmnet(x,y)
coef(cvfit, s = "lambda.1se")

Exercise 4.2

Each \(y_i\) is ’time to death’ for a particular leukemia patient (in weeks from diagnosis).

The \(x_i\) are \(\log_{10}\) initial white blood cell counts.

a.

x = c(3.36,2.88,3.63,3.41,3.78,4.02,4.00,4.23,3.73,3.85,3.97,4.51,4.54,5.00,5.00,4.72,5.00)
y = c(65,156,100,134,16,108,121,4,39,143,56,26,22,1,1,5,65)
plot(x,y)

The data shows a negative trend – as WBC increase, TTL decreases.

length(x) # 17 patients
[1] 17

b. \[ E(Y_i) = \exp(\beta_1 + \beta_2 x_i)\]

Read more >