# NOT RUN {
library(wsbackfit)
data(infect)
# Generalized varying coefficient model with binary response
m3 <- sback(formula = inf ~ sb(gluc, h = 10) + sb(gluc, by = linf, h = 10),
data = infect, family = "binomial", kbin = 15)
summary(m3)
# Plot both linear and non linear
# components of nonparametric functions: composed = FALSE
op <- par(no.readonly = TRUE)
par(mfrow = c(1,3))
plot(m3, composed = FALSE)
# Personalized plots
# First obtain predictions in new data
# Create newdata for prediction
ngrid <- 30
gluc0 <- seq(50, 190, length = ngrid)
linf0 <- seq(0, 45, length = ngrid)
df <- expand.grid(gluc = gluc0, linf = linf0)
m3p <- predict(m3, newdata = df)
par(mfrow = c(1,2))
ii <- order(df[,"gluc"])
## Parametric coefficients
names(m3p$coeff)
# Nonlinear components
colnames(m3p$peffects)
# Include the linear component
plot(df[ii,"gluc"], m3p$coeff[["gluc"]]*df[ii,"gluc"] +
m3p$peffects[ii,"sb(gluc, h = 10)"],
type = 'l', xlab = "Glucose (mg/dl)", ylab = "f_1(gluc)",
main = "Nonparametric effect of Glucose")
# Include the linear component
plot(df[ii,"gluc"], m3p$coeff[["gluc:linf"]]*df[ii,"gluc"] +
m3p$peffects[ii,"sb(gluc, h = 10, by = linf)"],
type= 'l', xlab = "Glucose (mg/dl)", ylab = "f_2(gluc)",
main = "Varying coefficients as a function of Glucose")
# Countour plot of the probability of post-opererational infection
n <- sqrt(nrow(df))
Z <- matrix(m3p$pfitted.values, n, n)
filled.contour(z = Z, x = gluc0, y = linf0,
xlab = "Glucose (mg/dl)", ylab = "Lymphocytes (%)",
main = "Probability of post-opererational infection")
par(op)
# }
Run the code above in your browser using DataLab