R Under development (unstable) (2024-08-16 r87026 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > suppressWarnings(RNGversion("3.5.2")) > > library("partykit") Loading required package: grid Loading required package: libcoin Loading required package: mvtnorm > > set.seed(29) > n <- 1000 > x <- runif(n) > z <- runif(n) > y <- rnorm(n, mean = x * c(-1, 1)[(z > 0.7) + 1], sd = 3) > z_noise <- factor(sample(1:3, size = n, replace = TRUE)) > d <- data.frame(y = y, x = x, z = z, z_noise = z_noise) > > > fmla <- as.formula("y ~ x | z + z_noise") > > (m_mob <- mob(formula = fmla, data = d, fit = partykit:::lmfit)) Model-based recursive partitioning (partykit:::lmfit) Model formula: y ~ x | z + z_noise Fitted party: [1] root | [2] z <= 0.70311: n = 704 | (Intercept) x | -0.1619978 -0.7896293 | [3] z > 0.70311: n = 296 | (Intercept) x | 0.08683535 0.65598287 Number of inner nodes: 1 Number of terminal nodes: 2 Number of parameters per node: 2 Objective function: 9635.95 > (m_lm2 <- lmtree(formula = fmla, data = d)) Linear model tree Model formula: y ~ x | z + z_noise Fitted party: [1] root | [2] z <= 0.70311: n = 704 | (Intercept) x | -0.1619978 -0.7896293 | [3] z > 0.70311: n = 296 | (Intercept) x | 0.08683535 0.65598287 Number of inner nodes: 1 Number of terminal nodes: 2 Number of parameters per node: 2 Objective function (residual sum of squares): 9635.95 > > > mods <- nodeapply(m_lm2, ids = nodeids(m_lm2, terminal = TRUE), function(x) x$info$object) > sum(sapply(mods, function(x) sum(x$residuals^2))) [1] 9635.95 > > > proc.time() user system elapsed 1.40 0.18 1.56