We generate data from a very simple model.
For each individual j=1,…,N, we generate i=1,…n observations of covariates Xi,j∈Rp, where the covariates are generated independently from a uniform distribution U[0,1]. The outcome is constructed by the following linear model Yi,j=XTi,jβj+εi,j, where εi,j∼N(0,1) and βj=(1⋮1)I{j≤N/2}.
set.seed(42)
n <- 100; N <- 10; p <- 3
X <- matrix(runif(N*n*p),nrow = n*N, ncol = p)
ind <- as.factor(rep(1:N,n)[sample(1:(n*N),n*N)])
Y <- X%*%rep(1,p) * (as.numeric(ind) <= N/2) + rnorm(n*N,0,1)
data <- data.frame(X,"y" = Y, "ind" = ind)
The data is saved into a standard data.frame()
, with a column “ind” (as a factor()
) which identifies the individuals.
At first, we have to initialize the adaptive discrete smoothing model with a valid learner. Calling the ADS$fit()
method estimates the corresponding parameters.
library(AdaptiveDiscreteSmoothing)
library(mlr3)
library(mlr3learners)
learner <- mlr_learners$get("regr.lm")
model <- ADS$new(data = data,
target = "y",
individ = "ind",
learner = learner,
iterations = 4)
model$fit()
Finally, we can predict new values by using the ADS$predict()
method.
One can easily take a look at the estimated weights via the ADS$heatmap()
method
model$heatmap()
For each individual j=1,…,N, we generate i=1,…n observations of covariates Xi,j∈Rp, where the covariates are generated by a normal distribution N(0,Σ) (here we consider Σ=Ip). The outcome is constructed by the following linear model Yi,j=XTi,jβj+εi,j, where εi,j∼N(0,σ2ε). The coefficient vector βj is generated to depend on an unobserved quantity αj∼U[0,1] βj=(1⋮1)+(αjα2j,−αj−α2jαjαj/2αj/3⋮).
set.seed(42)
n <- 200; N <- 20; p <- 10
ind <- as.factor(rep(1:N,each = n))
alpha_vec <- sort(runif(N, min = 0, max = 1))
X <- matrix(nrow = 0, ncol = 10)
Y <- numeric()
for (individual in seq_len(N)){
X_ind <- matrix(rnorm(n*p,0,1), ncol = p)
X <- rbind(X,X_ind)
alpha <- alpha_vec[individual]
beta <- rep(1,p) + c(alpha,alpha^2,-alpha,-alpha^2,alpha*1/seq_len(p-4))
Y <- append(Y,X_ind%*%beta + rnorm(n,0,1))
}
data <- data.frame(X,"y" = Y, "ind" = ind)
The values of α are sorted increasingly, to be able to interpret the weights (which should be higher on the diagonal of the heatmap).
We can apply the same learners as above or work with different learners. For example we can use random forests from the ranger-package or
learner = mlr_learners$get("regr.ranger")
learner$param_set$values = list(num.trees = 50, mtry = 3)
model_rf <- ADS$new(data = data,
target = "y",
individ = "ind",
learner = learner,
iterations = 4)
model_rf$fit()
cross-validated elastic net from the glmnet-package.
model_glmnet <- ADS$new(data = data,
target = "y",
individ = "ind",
learner = mlr_learners$get("regr.cv_glmnet"),
iterations = 4)
model_glmnet$fit()
Next, we generate a test dataset data_test
with the same coefficients and evaluate the performance of the estimators.
print(mean((data_test$y - model_rf$predict(newdata = data_test))^2))
#> [1] 4.555694
print(mean((data_test$y - model_glmnet$predict(newdata = data_test))^2))
#> [1] 1.25361
model_rf$heatmap()
model_glmnet$heatmap()
For each individual j=1,…,N, we generate i=1,…n observations of covariates Xi,j∈Rp, where the covariates are generated by a normal distribution N(0,Σ) (here we consider Σ=Ip). The outcome is constructed by the following model Yi,j=2sin(αjXi,j,1)+α2jXi,j,2+εi,j, where εi,j∼N(0,σ2ε) and αj∼U[0,1].
set.seed(42)
n <- 200; N <- 10; p <- 10
ind <- as.factor(rep(1:N,each = n))
alpha_vec <- sort(runif(N, min = 0, max = 1))
X <- matrix(nrow = 0, ncol = 10)
Y <- numeric()
for (individual in seq_len(N)){
X_ind <- matrix(rnorm(n*p,0,1), ncol = p)
X <- rbind(X,X_ind)
alpha <- alpha_vec[individual]
Y <- append(Y,2*sin(alpha*X_ind[,1])+alpha^2*X_ind[,2]+rnorm(n,0,1))
}
data <- data.frame(X,"y" = Y, "ind" = ind)
learner = mlr_learners$get("regr.ranger")
learner$param_set$values = list(num.trees = 50, mtry = 3)
model_rf <- ADS$new(data = data,
target = "y",
individ = "ind",
learner = learner,
iterations = 4)
model_rf$fit()
model_rf$heatmap()