Skip to contents

"fastcpd_lasso" and "fastcpd.lasso" are wrapper functions of fastcpd to find change points in penalized linear regression models. The function is similar to "fastcpd" except that the data is by default a matrix or data frame with the response variable as the first column and thus a formula is not required here.

Usage

fastcpd_lasso(data, ...)

fastcpd.lasso(data, ...)

Arguments

data

A matrix or a data frame with the response variable as the first column.

...

Other arguments passed to fastcpd, for example, segment_count.

Value

A class fastcpd object.

Examples

# \donttest{
for (package in c("ggplot2", "mvtnorm")) {
  if (!requireNamespace(package, quietly = TRUE)) utils::install.packages(
    package, repos = "https://cloud.r-project.org", quiet = TRUE
  )
}

set.seed(1)
n <- 480
p_true <- 6
p <- 50
x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p))
theta_0 <- rbind(
  runif(p_true, -5, -2),
  runif(p_true, -3, 3),
  runif(p_true, 2, 5),
  runif(p_true, -5, 5)
)
theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4))
y <- c(
  x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1),
  x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1),
  x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1),
  x[321:n, ] %*% theta_0[4, ] + rnorm(160, 0, 1)
)
result <- fastcpd.lasso(
  cbind(y, x),
  multiple_epochs = function(segment_length) if (segment_length < 30) 1 else 0
)
summary(result)
#> 
#> Call:
#> fastcpd.lasso(data = cbind(y, x), multiple_epochs = function(segment_length) if (segment_length < 
#>     30) 1 else 0)
#> 
#> Change points:
#> 80 200 321 
#> 
#> Cost values:
#> 266.8782 287.9668 328.5159 404.5794 
#> 
#> Parameters:
#> 50 x 4 sparse Matrix of class "dgCMatrix"
#>       segment 1  segment 2 segment 3 segment 4
#>  [1,] -1.944819 -1.9494841  4.144977 -2.467450
#>  [2,] -1.800555 -1.7048814  3.447478  2.276368
#>  [3,] -3.450190  0.7004564  4.408074  2.943917
#>  [4,] -4.152076  .          1.872264 -2.593882
#>  [5,] -4.052206  .          3.959711  2.403336
#>  [6,] -3.295512 -1.8637019  2.496156  3.410347
#>  [7,]  .         .          .         .       
#>  [8,]  .         .          .         .       
#>  [9,]  .         .          .         .       
#> [10,]  .         .          .         .       
#> [11,]  .         .          .         .       
#> [12,]  .         .          .         .       
#> [13,]  .         .          .         .       
#> [14,]  .         .          .         .       
#> [15,]  .         .          .         .       
#> [16,]  .         .          .         .       
#> [17,]  .         .          .         .       
#> [18,]  .         .          .         .       
#> [19,]  .         .          .         .       
#> [20,]  .         .          .         .       
#> [21,]  .         .          .         .       
#> [22,]  .         .          .         .       
#> [23,]  .         .          .         .       
#> [24,]  .         .          .         .       
#> [25,]  .         .          .         .       
#> [26,]  .         .          .         .       
#> [27,]  .         .          .         .       
#> [28,]  .         .          .         .       
#> [29,]  .         .          .         .       
#> [30,]  .         .          .         .       
#> [31,]  .         .          .         .       
#> [32,]  .         .          .         .       
#> [33,]  .         .          .         .       
#> [34,]  .         .          .         .       
#> [35,]  .         .          .         .       
#> [36,]  .         .          .         .       
#> [37,]  .         .          .         .       
#> [38,]  .         .          .         .       
#> [39,]  .         .          .         .       
#> [40,]  .         .          .         .       
#> [41,]  .         .          .         .       
#> [42,]  .         .          .         .       
#> [43,]  .         .          .         .       
#> [44,]  .         .          .         .       
#> [45,]  .         .          .         .       
#> [46,]  .         .          .         .       
#> [47,]  .         .          .         .       
#> [48,]  .         .          .         .       
#> [49,]  .         .          .         .       
#> [50,]  .         .          .         .       
plot(result)

# }