## Simulate some data
n <- 200
x1 <- rnorm(n)
x2 <- rnorm(n)
x3 <- rnorm(n)
x4 <- rnorm(n)
mu <- 4 * exp(8 * x1) / (1 + exp(8 * x1)) + exp(x3)
y <- mu + rnorm(n)
df1 <- data.frame(y, x1, x2, x3, x4)
## Fit an unconstrained the model
ans <- cgaim(y ~ g(x1, x2, label = "foo") + g(x3, x4, label = "bar"),
data = df1)
## Get fitted values
yhat <- predict(ans)
## Predict on new data
newdf <- as.data.frame(matrix(rnorm(100), 25, 4))
names(newdf) <- sprintf("x%i", 1:4)
# predicted response
ypred <- predict(ans, newdf)
# Indices
indices <- predict(ans, newdata = newdf, type = "indices")
# Ridge functions
funs <- predict(ans, newdata = newdf, type = "terms")
## Select specific terms
ind1 <- predict(ans, newdata = newdf, select = "foo", type = "indices")
fun1 <- predict(ans, newdata = newdf, select = "foo", type = "terms")
# Plot
plot(ans, select = "foo")
points(ind1, fun1)
## Scaled terms
fun2 <- predict(ans, newdata = newdf, select = "foo", type = "scterms")
# Plot
plot(ans, select = "foo", yscale = TRUE)
points(ind1, fun2)
Run the code above in your browser using DataLab