Assuming I have a set of p=n_features
, here set to 3 independent variables, each with n=n_samples
, without any missing values, defining my design matrix $X$ as follows:
$X = \begin{bmatrix}
x_{11} & \dots & x_{1p} \\
\vdots & \ddots & \vdots \\
x_{n1} & \dots & x_{np}
\end{bmatrix}$
For my dataset with p=3
features:
$X=\left[\vec{x_1},\ \vec{x_2},\ \vec{x_3}\right]$
The variables are of the following kinds:
- $y$, the dependent variable: continuous numeric variable
- $x_1$ and $x_2$: continuous numeric variables, with different value ranges requiring standardization/scaling due to l1/l2 regularization
- $x_3$: categorical numeric variable with the 3 levels $\left[0,1,2\right]$, requiring dummy coding/one hot encoding into $k-1=2$ binary dummy variables
I want to feed this dataset into a polynomial regression of second degree with interaction terms (also regularization is applied), meaning my linear model to fit is of the following form:
$y=c + c_1x_1 + c_2x_2 + c_3x_3 + c_4x_1x_2 + c_5x_1x_3 + c_6x_2x_3 + c_7x_1^2 + c_8x_2^2 + c_9x_3^2 + \vec{\epsilon}$
with the intercept $c$, the coefficients $c_1\dots c_9$ and the error $\vec{\epsilon}$.
A polynomial transformation of the design matrix yields the transformed design matrix $X^*$:
$X^*=\left[\vec{x_1^*},\ \vec{x_2^*},\ \vec{x_3^*},\ \vec{x_4^*},\ \vec{x_5^*},\ \vec{x_6^*},\ \vec{x_7^*},\ \vec{x_8^*},\ \vec{x_9^*}\right]$
with $\vec{x_1^*}=\vec{x_1},\quad \dots,\quad \vec{x_4^*}=\vec{x_1}\vec{x_2},\quad \vec{x_5^*}=\vec{x_1}\vec{x_3},\quad \dots \vec{x_9^*}=\vec{x_3^2}$
Problem description
We now have interaction terms between continuous and categorical variables, namely $c_5x_1x_3$ and $c_6x_2x_3$.
Dummy coding of the categoric variable has not yet been performed! (More polynomial terms if done before transformation.)
Standardization of the cont. indep. variables still needs to be done!
Having a model only consisting of continuous variables, I'd standardize after poly. transformation in most cases. In this case, with mixed types of indep. variables, I'd standardize the continuous variables and dummy code the categorical variables before polynomial transformation.
Questions
- Should I standardize and dummy code after polynomial transformation?
- If yes, how to deal with the interaction terms of categorical and continuous variables?
- If yes, how serious are the disadvantages introduced with standardizing/dummy coding before poly. transf.?
- In general: How to avoid alternating signs (making "random" negative values) by subtracting the mean and multiplying for interaction terms (f.i. $x_1x_2$ where both $x_1$ and $x_2$ were positive before standardization, but afterwards $x_1$ is negative)? Just scale by the standard deviation $\sigma$ (and possibly min-max-scale afterwards)?
sklearn
/scikit-learn
does not support restricted cubic splines. This is also the reason why I don't have any experience with cubic spline fitting. Or is there any restricted cubic spline method insklearn
, which does not require splitting up the paths separately, with f.i. decision trees? $\endgroup$sklearn
, so I can't comment on that. In R therms
package provides restricted cubic splines easily. Think carefully about whether and how to standardize the categorical predictor; see this answer for an introduction to the problems, which are even greater with more than 2 levels, and its links for further study. Also think about what you hope to gain from including all the high-order interaction terms. Sometimes applying knowledge of the subject matter can simplify the model. $\endgroup$