Hi everyone!
I have a very basic question but I maybe you could help me.
Suppose you have two random variables
X1 <- rnorm(1000)
X2 <- rnorm(1000)
and I want to estimate the 3 knot restricted cubic spline for each one. Therefore, I put this code
r1 <- rcs(X1, 4)
r2 <- rcs(X2, 4)
However, these functions returns 1000x4
matrices with some properties. However rcs should also return a 1000x1
vector with the final estimation given by the equation (2.24) from the Regression Modelling Strategies book.
My best guess is that I can recover the splines doing this:
cbind(1, r1) %*% attr(r1, "parms")
cbind(1, r2) %*% attr(r2, "parms")
However if I try to estimate the restricted interaction of r1
and r2
I don’t know how to retrieve the final vector of the spline, because the “parms” attribute indicates where is positioned X1 and X2, but not the coefficients.
head(r1 %ia% r2)
X1 * X2 X1 * X2' X1 * X2'' X1 * X2''' X1' * X2 X1'' * X2
[1,] -0.012547279 -0.10601003 -5.263230e-03 -5.189006e-06 0.017786810 4.460467e-05
[2,] -0.073957398 0.01408416 0.000000e+00 0.000000e+00 -0.313116000 -7.556321e-03
[3,] -0.218514107 0.48459929 9.975158e-03 0.000000e+00 -0.354030733 -5.246242e-02
[4,] 0.422954923 -0.12262768 -7.746083e-06 0.000000e+00 -0.030438609 0.000000e+00
[5,] 0.003644586 -0.07949653 -2.991382e-03 0.000000e+00 -0.006242986 -2.130929e-05
[6,] -1.805530149 -3.47596715 -6.373479e-01 -1.349836e-01 0.000000000 0.000000e+00
attr(,"ia")
[1] "X1" "X2"
attr(,"parms")
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 0 0 0 0 0 1 1
[2,] 0 0 1 1 1 0 0
attr(,"nonlinear")
[1] FALSE TRUE TRUE TRUE TRUE TRUE
attr(,"assume.code")
[1] 9
attr(,"name")
[1] "X1 * X2"
attr(,"label")
[1] "X1 * X2"
attr(,"iaspecial")
[1] FALSE
attr(,"colnames")
[1] "X1 * X2" "X1 * X2'" "X1 * X2''" "X1 * X2'''" "X1' * X2" "X1'' * X2"
attr(,"class")
[1] "rms"
Could anyone help me to estimate this vector?
Thanks.