Date: 2025-07-31

# Using the SplitWise Package with the mtcars Dataset

#### 2025-07-31

# Introduction

The `SplitWise` package provides tools for transforming numeric variables in regression models by either applying a single-split dummy encoding or retaining them as linear terms. This vignette demonstrates the application of `SplitWise` using the `mtcars` dataset, showcasing both univariate and iterative transformation approaches.

# The mtcars Dataset

The `mtcars` dataset is a built-in R dataset that comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).

    # Load the mtcars dataset
    data(mtcars)

# Iterative Transformations

The iterative transformation approach evaluates each variable’s transformation in the context of variables already added to the model. Here is an example using forward stepwise selection:

    # Apply iterative transformations with forward stepwise selection
    model_iter <- splitwise(
      mpg ~ .,
      data = mtcars,
      transformation_mode = "iterative",
      direction = "backward",
      verbose = FALSE
    )

    # Display the summary of the model
    summary(model_iter)
    #>
    #> Call:
    #> stats::lm(formula = mpg ~ cyl + disp_dummy + hp + drat_dummy +
    #>     gear, data = df_final)
    #>
    #> Residuals:
    #>     Min      1Q  Median      3Q     Max
    #> -3.5590 -0.9388 -0.0726  1.4395  3.0073
    #>
    #> Coefficients:
    #>              Estimate Std. Error t value Pr(>|t|)
    #> (Intercept) 27.239683   3.489799   7.806 2.80e-08 ***
    #> cyl         -0.594491   0.437232  -1.360  0.18561
    #> disp_dummy  -7.047186   0.975464  -7.224 1.13e-07 ***
    #> hp          -0.033663   0.009916  -3.395  0.00221 **
    #> drat_dummy   3.490007   0.971554   3.592  0.00134 **
    #> gear         1.182249   0.594126   1.990  0.05722 .
    #> ---
    #> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    #>
    #> Residual standard error: 1.679 on 26 degrees of freedom
    #> Multiple R-squared:  0.9349, Adjusted R-squared:  0.9224
    #> F-statistic: 74.73 on 5 and 26 DF,  p-value: 1.377e-14
    #>
    #> Transformation Mode: iterative
    #>
    #> Dummy-Encoded Variables:
    #>   - disp : 1 if x >= 101.550 ; else 0
    #>   - drat : 1 if x >= 3.035 ; else 0
    #>   - wt : 1 if 1.885 < x < 3.843 ; else 0
    #>   - qsec : 1 if 17.175 < x < 19.170 ; else 0
    #>   - carb : 1 if 3.500 < x < 5.000 ; else 0
    #>
    #> Final AIC: 131.31
    #> Final BIC: 141.58

    # Print the model details
    print(model_iter)
    #> SplitWise Linear Models
    #> Transformation mode: iterative
    #> Call:
    #> splitwise(formula = mpg ~ ., data = mtcars, transformation_mode = "iterative",
    #>     direction = "backward", verbose = FALSE)
    #>
    #> Dummy-Encoded Variables:
    #>   - disp : 1 if x >=  101.550 ; else 0
    #>   - drat : 1 if x >=  3.035 ; else 0
    #>   - wt : 1 if  1.885  < x <  3.843 ; else 0
    #>   - qsec : 1 if  17.175  < x <  19.17 ; else 0
    #>   - carb : 1 if  3.5  < x <  5 ; else 0
    #>
    #> Coefficients:
    #> (Intercept)         cyl  disp_dummy          hp  drat_dummy        gear
    #> 27.23968299 -0.59449092 -7.04718608 -0.03366259  3.49000675  1.18224888
    #>
    #> Model Fit Statistics:
    #>   AIC: 131.31
    #>   BIC: 141.58

# Univariate Transformations

In the univariate transformation approach, each numeric predictor is transformed independently without considering the context of other variables. Below is an example of applying univariate transformations with backward stepwise selection:

    # Apply univariate transformations with backward stepwise selection
    model_uni <- splitwise(
      mpg ~ .,
      data = mtcars,
      transformation_mode = "univariate",
      direction = "backward",
      verbose = FALSE
    )

    # Display the summary of the model
    summary(model_uni)
    #>
    #> Call:
    #> stats::lm(formula = mpg ~ cyl + hp + wt, data = df_final)
    #>
    #> Residuals:
    #>     Min      1Q  Median      3Q     Max
    #> -3.9290 -1.5598 -0.5311  1.1850  5.8986
    #>
    #> Coefficients:
    #>             Estimate Std. Error t value Pr(>|t|)
    #> (Intercept) 38.75179    1.78686  21.687  < 2e-16 ***
    #> cyl         -0.94162    0.55092  -1.709 0.098480 .
    #> hp          -0.01804    0.01188  -1.519 0.140015
    #> wt          -3.16697    0.74058  -4.276 0.000199 ***
    #> ---
    #> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    #>
    #> Residual standard error: 2.512 on 28 degrees of freedom
    #> Multiple R-squared:  0.8431, Adjusted R-squared:  0.8263
    #> F-statistic: 50.17 on 3 and 28 DF,  p-value: 2.184e-11
    #>
    #> Transformation Mode: univariate
    #>
    #> Dummy-Encoded Variables:
    #>   - qsec : 1 if 17.175 < x < 18.410 ; else 0
    #>   - gear : 1 if x >= 3.500 ; else 0
    #>
    #> Final AIC: 155.48
    #> Final BIC: 162.81

    # Print the model details
    print(model_uni)
    #> SplitWise Linear Models
    #> Transformation mode: univariate
    #> Call:
    #> splitwise(formula = mpg ~ ., data = mtcars, transformation_mode = "univariate",
    #>     direction = "backward", verbose = FALSE)
    #>
    #> Dummy-Encoded Variables:
    #>   - qsec : 1 if  17.175  < x <  18.41 ; else 0
    #>   - gear : 1 if x >=  3.500 ; else 0
    #>
    #> Coefficients:
    #> (Intercept)         cyl          hp          wt
    #>  38.7517874  -0.9416168  -0.0180381  -3.1669731
    #>
    #> Model Fit Statistics:
    #>   AIC: 155.48
    #>   BIC: 162.81

# Conclusion

This vignette illustrated how to utilize the `SplitWise` package to perform both univariate and iterative transformations on the `mtcars` dataset. Depending on the analysis requirements, users can choose the appropriate transformation approach to enhance their regression models.