# NOT RUN {
# Testing for nonlinearity: Logistic link function
set.seed(1)
X <- matrix(rnorm(300*30), 300, 30)
z <- X[, 1] + X[, 2]^4
pr <- 1/(1 + exp(-z))
y <- rbinom(nrow(X), 1, pr)
(out <- GRPtest(X, y, fam = "binomial", nsplits = 5))
# Testing for nonlinearity: Define your own RP function
# use package xyz
my_RP_function <- function(XA, resA, XB){
xyz_fit <- xyz_regression(XA, resA)
predict(xyz_fit, newdata = as.matrix(XB))[,5]
}
library(xyz)
set.seed(2)
X <- matrix(rnorm(500*30), 500, 30)
z <- X[,1:3]%*%rep(1,3) + 1*X[, 1]*X[,5]
mu <- exp(z)
y <- rpois(n = nrow(X), lambda = mu)
(out <- GRPtest(X, y, fam = "poisson", RP_function = my_RP_function))
# }
# NOT RUN {
# An example with factors (labelled as "Not run" due to running time > 10s)
set.seed(1)
n <- 2021
X1 <- sample(c("A","B","C"), n, replace = TRUE)
X1 <- factor(X1, levels = c("A", "B", "C"))
X2 <- sample(c("Male","Female"), n, replace = TRUE)
X2 <- factor(X2, levels = c("Male", "Female"))
X3 <- rnorm(n)
X <- data.frame(X1, X2, X3)
# Generate response y1 using a logistic regression model
prob1 <- 1 / (1 + exp( - (X1 == "B") + 2*(X1 == "C") - 2*(X2 == "Male") - X3) )
y1 <- rbinom(n, 1, prob1)
# Output p-value for goodness of fit of the logistic regression model
(out <- GRPtest(X, y1, fam = "binomial", nsplits = 10))
# Generate response y2 using a logistic regression model but with an interaction between X1 and X2
prob2 <- 1 / (1 + exp( - (X1 == "B") + 2*(X1 == "C") + 2*(X1 == "B")*(X2 == "Male") - 0.5*X3) )
y2 <- rbinom(n, 1, prob2)
# Test goodness of fit of the logistic regression model
(out <- GRPtest(X, y2, fam = "binomial", nsplits = 10))
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab