From fab5552225d9c8366ee40dd03d11d2c2c4f5606a Mon Sep 17 00:00:00 2001 From: Jannik Date: Tue, 9 Jan 2024 11:42:45 +0100 Subject: [PATCH] update vignettes --- ...finition-Variables-and-Multi-Group-SEM.Rmd | 132 +++++----- vignettes/General-Purpose-Optimization.Rmd | 171 ++++++------- vignettes/Mixed-Penalties.Rmd | 104 ++++---- vignettes/Parameter-transformations.Rmd | 239 +++++++++--------- vignettes/The-Structural-Equation-Model.Rmd | 216 ++++++++-------- vignettes/lessSEM.Rmd | 218 ++++++++-------- 6 files changed, 525 insertions(+), 555 deletions(-) diff --git a/vignettes/Definition-Variables-and-Multi-Group-SEM.Rmd b/vignettes/Definition-Variables-and-Multi-Group-SEM.Rmd index 2bb61cc..4dd1f7d 100644 --- a/vignettes/Definition-Variables-and-Multi-Group-SEM.Rmd +++ b/vignettes/Definition-Variables-and-Multi-Group-SEM.Rmd @@ -100,18 +100,18 @@ Let's have a look at the parameters: ```r coef(fit) -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> lambda alpha ||--|| l1_Pasteur l2_Pasteur l3_Pasteur v1 v2 v3 m1 m2 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== -#> 0.0000 0.0000 ||--|| 0.7240 0.5610 0.8824 0.8449 1.0711 0.6108 4.9212 6.0770 -#> -#> -#> ---------- -------------- -------------- -------------- -#> m3 l1_Grant_White l2_Grant_White l3_Grant_White -#> ========== ============== ============== ============== -#> 2.2281 0.7088 0.5536 0.7360 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> lambda alpha ||--|| l1_Pasteur l2_Pasteur l3_Pasteur v1 v2 v3 m1 m2 m3 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 0.0000 0.0000 ||--|| 0.7240 0.5610 0.8824 0.8449 1.0711 0.6108 4.9212 6.0770 2.2281 +#> +#> +#> -------------- -------------- -------------- +#> l1_Grant_White l2_Grant_White l3_Grant_White +#> ============== ============== ============== +#> 0.7088 0.5536 0.7360 ``` That's curious! There are group-specific parameters, but **only** for the parameters where we provided group-specific names! @@ -192,48 +192,48 @@ that we wanted these parameters to have exactly the same value in both models. ```r coef(fit) -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ----------- ----------- -#> lambda alpha ||--|| ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== =========== =========== -#> 0.0000 0.0000 ||--|| 1.8164 1.5582 1.1879 1.1706 1.2511 1.4062 0.4696 -#> -#> -#> ----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> dem65~dem60 y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 -#> =========== ========== ========== ========== ========== ========== ========== ========== ========== ========== -#> 0.8755 0.5389 1.4270 2.2120 0.7425 0.3718 1.3734 0.0000 1.3895 1.2086 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------------ -#> y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 -#> ========== ========== ========== ========== ========== ========== ========== ========== ============ -#> 1.8544 7.5981 4.9592 3.2006 2.2664 4.9911 3.6046 3.3135 0.5305 -#> -#> -#> ------------ ------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 -#> ============ ============ ========== ========== ========== ========== ========== ========== ========== -#> 3.8021 0.2003 5.0409 5.8482 2.5445 5.4463 4.2330 6.5420 4.4295 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ----------- ----------- ---------- ---------- -#> y5~1 y6~1 y7~1 y8~1 visual=~x2 visual=~x3 textual=~x5 textual=~x6 speed=~x8 speed=~x9 -#> ========== ========== ========== ========== ========== ========== =========== =========== ========== ========== -#> 5.1139 2.9502 6.1703 4.0150 0.2791 0.4461 1.1126 0.9221 1.1746 1.0056 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- -------------- ---------------- ------------ -#> x4~~x4 x5~~x5 x6~~x6 x7~~x7 x8~~x8 x9~~x9 visual~~visual textual~~textual speed~~speed -#> ========== ========== ========== ========== ========== ========== ============== ================ ============ -#> 0.3681 0.4434 0.3610 0.7750 0.4590 0.6025 1.3695 0.9839 0.4084 -#> -#> -#> --------------- ------------- -------------- ---------- ---------- ---------- ---------- ---------- ---------- -#> visual~~textual visual~~speed textual~~speed x4~1 x5~1 x6~1 x7~1 x8~1 x9~1 -#> =============== ============= ============== ========== ========== ========== ========== ========== ========== -#> 0.4666 0.2615 0.1746 3.0967 4.3804 2.2186 4.2060 5.5507 5.3943 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ----------- ----------- ----------- ---------- +#> lambda alpha ||--|| ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== =========== =========== =========== ========== +#> 0.0000 0.0000 ||--|| 1.8164 1.5582 1.1879 1.1706 1.2511 1.4062 0.4696 0.8755 0.5389 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 y2~~y2 y3~~y3 +#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 1.4270 2.2120 0.7425 0.3718 1.3734 0.0000 1.3895 1.2086 1.8544 7.5981 4.9592 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ------------ ------------ ------------ ---------- ---------- ---------- +#> y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 +#> ========== ========== ========== ========== ========== ============ ============ ============ ========== ========== ========== +#> 3.2006 2.2664 4.9911 3.6046 3.3135 0.5305 3.8021 0.2003 5.0409 5.8482 2.5445 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------- +#> y1~1 y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 visual=~x2 visual=~x3 textual=~x5 +#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== =========== +#> 5.4463 4.2330 6.5420 4.4295 5.1139 2.9502 6.1703 4.0150 0.2791 0.4461 1.1126 +#> +#> +#> ----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -------------- +#> textual=~x6 speed=~x8 speed=~x9 x4~~x4 x5~~x5 x6~~x6 x7~~x7 x8~~x8 x9~~x9 visual~~visual +#> =========== ========== ========== ========== ========== ========== ========== ========== ========== ============== +#> 0.9221 1.1746 1.0056 0.3681 0.4434 0.3610 0.7750 0.4590 0.6025 1.3695 +#> +#> +#> ---------------- ------------ --------------- ------------- -------------- ---------- ---------- ---------- ---------- +#> textual~~textual speed~~speed visual~~textual visual~~speed textual~~speed x4~1 x5~1 x6~1 x7~1 +#> ================ ============ =============== ============= ============== ========== ========== ========== ========== +#> 0.9839 0.4084 0.4666 0.2615 0.1746 3.0967 4.3804 2.2186 4.2060 +#> +#> +#> ---------- ---------- +#> x8~1 x9~1 +#> ========== ========== +#> 5.5507 5.3943 ``` ## Regularized Multi-Group Models @@ -254,18 +254,18 @@ The coefficients can be extracted as usual: ```r coef(fit, criterion = "AIC") -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> lambda alpha ||--|| l1_Pasteur l2_Pasteur l3_Pasteur v1 v2 v3 m1 m2 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== -#> 0.0000 1.0000 ||--|| 0.7239 0.5609 0.8826 0.8450 1.0712 0.6107 4.9212 6.0769 -#> -#> -#> ---------- -------------- -------------- -------------- -#> m3 l1_Grant_White l2_Grant_White l3_Grant_White -#> ========== ============== ============== ============== -#> 2.2280 0.7087 0.5536 0.7361 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> lambda alpha ||--|| l1_Pasteur l2_Pasteur l3_Pasteur v1 v2 v3 m1 m2 m3 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 0.0000 1.0000 ||--|| 0.7239 0.5609 0.8826 0.8450 1.0712 0.6107 4.9212 6.0769 2.2280 +#> +#> +#> -------------- -------------- -------------- +#> l1_Grant_White l2_Grant_White l3_Grant_White +#> ============== ============== ============== +#> 0.7087 0.5536 0.7361 ``` ### Regularizing Differences Between Parameters using **lessSEM** diff --git a/vignettes/General-Purpose-Optimization.Rmd b/vignettes/General-Purpose-Optimization.Rmd index 6e0a1f6..8ccd962 100644 --- a/vignettes/General-Purpose-Optimization.Rmd +++ b/vignettes/General-Purpose-Optimization.Rmd @@ -99,20 +99,13 @@ because we also want to estimate the intercept. To this end, we extend X: ```r Xext <- cbind(1,X) head(Xext) -#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] -#> [1,] 1 -0.56047565 -0.71040656 2.1988103 -0.7152422 -0.07355602 -0.60189285 1.07401226 -0.7282191 0.3562833 -#> [2,] 1 -0.23017749 0.25688371 1.3124130 -0.7526890 -1.16865142 -0.99369859 -0.02734697 -1.5404424 -0.6580102 -#> [3,] 1 1.55870831 -0.24669188 -0.2651451 -0.9385387 -0.63474826 1.02678506 -0.03333034 -0.6930946 0.8552022 -#> [4,] 1 0.07050839 -0.34754260 0.5431941 -1.0525133 -0.02884155 0.75106130 -1.51606762 0.1188494 1.1529362 -#> [5,] 1 0.12928774 -0.95161857 -0.4143399 -0.4371595 0.67069597 -1.50916654 0.79038534 -1.3647095 0.2762746 -#> [6,] 1 1.71506499 -0.04502772 -0.4762469 0.3311792 -1.65054654 -0.09514745 -0.21073418 0.5899827 0.1441047 -#> [,11] -#> [1,] -1.0141142 -#> [2,] -0.7913139 -#> [3,] 0.2995937 -#> [4,] 1.6390519 -#> [5,] 1.0846170 -#> [6,] -0.6245675 +#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] +#> [1,] 1 -0.56047565 -0.71040656 2.1988103 -0.7152422 -0.07355602 -0.60189285 1.07401226 -0.7282191 0.3562833 -1.0141142 +#> [2,] 1 -0.23017749 0.25688371 1.3124130 -0.7526890 -1.16865142 -0.99369859 -0.02734697 -1.5404424 -0.6580102 -0.7913139 +#> [3,] 1 1.55870831 -0.24669188 -0.2651451 -0.9385387 -0.63474826 1.02678506 -0.03333034 -0.6930946 0.8552022 0.2995937 +#> [4,] 1 0.07050839 -0.34754260 0.5431941 -1.0525133 -0.02884155 0.75106130 -1.51606762 0.1188494 1.1529362 1.6390519 +#> [5,] 1 0.12928774 -0.95161857 -0.4143399 -0.4371595 0.67069597 -1.50916654 0.79038534 -1.3647095 0.2762746 1.0846170 +#> [6,] 1 1.71506499 -0.04502772 -0.4762469 0.3311792 -1.65054654 -0.09514745 -0.21073418 0.5899827 0.1441047 -0.6245675 ``` Finally, we need to decide which parameters should be regularized and the values for @@ -143,20 +136,20 @@ head(l1@parameters) ``` ``` -#> lambda alpha theta b0 b1 b2 b3 b4 b5 b6 b7 -#> 1 0.000000000 1 0 0.02738472 1.0129194 0.9991454 0.9705725 1.027626 0.014036009 -0.007460964 0.0185899238 -#> 2 0.005263158 1 0 0.02935302 1.0043737 0.9908934 0.9626258 1.025138 0.003365832 0.000000000 0.0143411319 -#> 3 0.010526316 1 0 0.02995132 0.9967095 0.9846674 0.9552789 1.021891 0.000000000 0.000000000 0.0096220740 -#> 4 0.015789474 1 0 0.03010607 0.9897339 0.9789423 0.9481496 1.018672 0.000000000 0.000000000 0.0049333012 -#> 5 0.021052632 1 0 0.03029739 0.9827288 0.9732058 0.9409861 1.015363 0.000000000 0.000000000 0.0001782005 -#> 6 0.026315789 1 0 0.03112551 0.9753354 0.9670622 0.9338614 1.011552 0.000000000 0.000000000 0.0000000000 -#> b8 b9 b10 -#> 1 0.021930771 -0.009900077 0.027401044 -#> 2 0.015434822 -0.007939443 0.022297575 -#> 3 0.010707768 -0.005256845 0.017465446 -#> 4 0.006364327 -0.002392921 0.012713123 -#> 5 0.002036124 0.000000000 0.007969754 -#> 6 0.000000000 0.000000000 0.003304080 +#> lambda alpha theta b0 b1 b2 b3 b4 b5 b6 b7 b8 +#> 1 0.000000000 1 0 0.02738472 1.0129194 0.9991454 0.9705725 1.027626 0.014036009 -0.007460964 0.0185899238 0.021930771 +#> 2 0.005263158 1 0 0.02935302 1.0043737 0.9908934 0.9626258 1.025138 0.003365832 0.000000000 0.0143411319 0.015434822 +#> 3 0.010526316 1 0 0.02995132 0.9967095 0.9846674 0.9552789 1.021891 0.000000000 0.000000000 0.0096220740 0.010707768 +#> 4 0.015789474 1 0 0.03010607 0.9897339 0.9789423 0.9481496 1.018672 0.000000000 0.000000000 0.0049333012 0.006364327 +#> 5 0.021052632 1 0 0.03029739 0.9827288 0.9732058 0.9409861 1.015363 0.000000000 0.000000000 0.0001782005 0.002036124 +#> 6 0.026315789 1 0 0.03112551 0.9753354 0.9670622 0.9338614 1.011552 0.000000000 0.000000000 0.0000000000 0.000000000 +#> b9 b10 +#> 1 -0.009900077 0.027401044 +#> 2 -0.007939443 0.022297575 +#> 3 -0.005256845 0.017465446 +#> 4 -0.002392921 0.012713123 +#> 5 0.000000000 0.007969754 +#> 6 0.000000000 0.003304080 ``` @@ -192,20 +185,20 @@ head(l1@parameters) ``` ``` -#> lambda alpha theta b0 b1 b2 b3 b4 b5 b6 b7 -#> 1 0.000000000 1 0 0.02738485 1.0129200 0.9991452 0.9705725 1.027626 0.014034994 -0.007460252 0.0185901898 -#> 2 0.005263158 1 0 0.02935325 1.0043732 0.9908928 0.9626258 1.025139 0.003364951 0.000000000 0.0143418480 -#> 3 0.010526316 1 0 0.02995023 0.9967094 0.9846669 0.9552792 1.021892 0.000000000 0.000000000 0.0096217574 -#> 4 0.015789474 1 0 0.03010649 0.9897330 0.9789426 0.9481493 1.018672 0.000000000 0.000000000 0.0049332838 -#> 5 0.021052632 1 0 0.03029729 0.9827286 0.9732062 0.9409869 1.015362 0.000000000 0.000000000 0.0001772169 -#> 6 0.026315789 1 0 0.03112481 0.9753368 0.9670620 0.9338616 1.011553 0.000000000 0.000000000 0.0000000000 -#> b8 b9 b10 -#> 1 0.021930702 -0.009900699 0.027400748 -#> 2 0.015434447 -0.007939640 0.022297136 -#> 3 0.010707383 -0.005257048 0.017465134 -#> 4 0.006363868 -0.002393522 0.012713116 -#> 5 0.002036300 0.000000000 0.007969996 -#> 6 0.000000000 0.000000000 0.003303777 +#> lambda alpha theta b0 b1 b2 b3 b4 b5 b6 b7 b8 +#> 1 0.000000000 1 0 0.02738485 1.0129200 0.9991452 0.9705725 1.027626 0.014034994 -0.007460252 0.0185901898 0.021930702 +#> 2 0.005263158 1 0 0.02935325 1.0043732 0.9908928 0.9626258 1.025139 0.003364951 0.000000000 0.0143418480 0.015434447 +#> 3 0.010526316 1 0 0.02995023 0.9967094 0.9846669 0.9552792 1.021892 0.000000000 0.000000000 0.0096217574 0.010707383 +#> 4 0.015789474 1 0 0.03010649 0.9897330 0.9789426 0.9481493 1.018672 0.000000000 0.000000000 0.0049332838 0.006363868 +#> 5 0.021052632 1 0 0.03029729 0.9827286 0.9732062 0.9409869 1.015362 0.000000000 0.000000000 0.0001772169 0.002036300 +#> 6 0.026315789 1 0 0.03112481 0.9753368 0.9670620 0.9338616 1.011553 0.000000000 0.000000000 0.0000000000 0.000000000 +#> b9 b10 +#> 1 -0.009900699 0.027400748 +#> 2 -0.007939640 0.022297136 +#> 3 -0.005257048 0.017465134 +#> 4 -0.002393522 0.012713116 +#> 5 0.000000000 0.007969996 +#> 6 0.000000000 0.003303777 ``` Here is a short comparison of running both models 5 times each: @@ -214,13 +207,13 @@ Here is a short comparison of running both models 5 times each: Runtime in seconds without gradients: ``` -#> [1] 0.3024640 0.3344960 0.3050129 0.3076599 0.2936771 +#> [1] 0.2155101 0.2029781 0.1960189 0.2026141 0.2020009 ``` Runtime in seconds with gradients: ``` -#> [1] 0.03109193 0.03083801 0.04264498 0.03115797 0.03234506 +#> [1] 0.02016687 0.02007699 0.02003384 0.01954794 0.02730393 ``` That's quite a speedup! @@ -290,20 +283,20 @@ head(l1@parameters) ``` -#> lambda alpha theta b0 b1 b2 b3 b4 b5 b6 b7 -#> 1 0.000000000 1 0 0.02738527 1.0129194 0.9991448 0.9705722 1.027624 0.014035744 -0.007459583 0.0185892822 -#> 2 0.005263158 1 0 0.02935316 1.0043741 0.9908936 0.9626259 1.025139 0.003366124 0.000000000 0.0143421453 -#> 3 0.010526316 1 0 0.02995019 0.9967089 0.9846670 0.9552796 1.021892 0.000000000 0.000000000 0.0096214361 -#> 4 0.015789474 1 0 0.03010669 0.9897326 0.9789426 0.9481493 1.018672 0.000000000 0.000000000 0.0049330908 -#> 5 0.021052632 1 0 0.03029700 0.9827285 0.9732063 0.9409868 1.015362 0.000000000 0.000000000 0.0001764845 -#> 6 0.026315789 1 0 0.03112464 0.9753365 0.9670623 0.9338615 1.011553 0.000000000 0.000000000 0.0000000000 -#> b8 b9 b10 -#> 1 0.021930374 -0.009900549 0.027401230 -#> 2 0.015435255 -0.007938658 0.022297962 -#> 3 0.010707812 -0.005256606 0.017465214 -#> 4 0.006364232 -0.002393757 0.012713307 -#> 5 0.002037008 0.000000000 0.007971003 -#> 6 0.000000000 0.000000000 0.003304894 +#> lambda alpha theta b0 b1 b2 b3 b4 b5 b6 b7 b8 +#> 1 0.000000000 1 0 0.02738527 1.0129194 0.9991448 0.9705722 1.027624 0.014035744 -0.007459583 0.0185892822 0.021930374 +#> 2 0.005263158 1 0 0.02935316 1.0043741 0.9908936 0.9626259 1.025139 0.003366124 0.000000000 0.0143421453 0.015435255 +#> 3 0.010526316 1 0 0.02995019 0.9967089 0.9846670 0.9552796 1.021892 0.000000000 0.000000000 0.0096214361 0.010707812 +#> 4 0.015789474 1 0 0.03010669 0.9897326 0.9789426 0.9481493 1.018672 0.000000000 0.000000000 0.0049330908 0.006364232 +#> 5 0.021052632 1 0 0.03029700 0.9827285 0.9732063 0.9409868 1.015362 0.000000000 0.000000000 0.0001764845 0.002037008 +#> 6 0.026315789 1 0 0.03112464 0.9753365 0.9670623 0.9338615 1.011553 0.000000000 0.000000000 0.0000000000 0.000000000 +#> b9 b10 +#> 1 -0.009900549 0.027401230 +#> 2 -0.007938658 0.022297962 +#> 3 -0.005256606 0.017465214 +#> 4 -0.002393757 0.012713307 +#> 5 0.000000000 0.007971003 +#> 6 0.000000000 0.003304894 ``` @@ -311,7 +304,7 @@ head(l1@parameters) The runtime in seconds with C++ is: ``` -#> [1] 0.02511597 0.02394223 0.02351594 0.03576016 0.02308798 +#> [1] 0.01715302 0.01618695 0.01546001 0.01603198 0.01513100 ``` Which is even lower than what we had before! @@ -598,20 +591,20 @@ head(l1@parameters) ``` -#> lambda alpha theta b0 b1 b2 b3 b4 b5 b6 b7 -#> 1 0.000000000 1 0 0.02738542 1.0129198 0.9991455 0.9705733 1.027625 0.014037188 -0.007460885 0.0185907073 -#> 2 0.005263158 1 0 0.02935271 1.0043736 0.9908928 0.9626259 1.025139 0.003365862 0.000000000 0.0143413234 -#> 3 0.010526316 1 0 0.02995027 0.9967094 0.9846668 0.9552792 1.021892 0.000000000 0.000000000 0.0096220212 -#> 4 0.015789474 1 0 0.03010668 0.9897329 0.9789425 0.9481493 1.018672 0.000000000 0.000000000 0.0049333261 -#> 5 0.021052632 1 0 0.03029739 0.9827288 0.9732059 0.9409868 1.015363 0.000000000 0.000000000 0.0001773121 -#> 6 0.026315789 1 0 0.03112461 0.9753368 0.9670620 0.9338617 1.011553 0.000000000 0.000000000 0.0000000000 -#> b8 b9 b10 -#> 1 0.021930984 -0.009900023 0.027401255 -#> 2 0.015434697 -0.007939417 0.022297387 -#> 3 0.010707439 -0.005256688 0.017464767 -#> 4 0.006364019 -0.002393495 0.012713141 -#> 5 0.002036026 0.000000000 0.007969742 -#> 6 0.000000000 0.000000000 0.003303734 +#> lambda alpha theta b0 b1 b2 b3 b4 b5 b6 b7 b8 +#> 1 0.000000000 1 0 0.02738542 1.0129198 0.9991455 0.9705733 1.027625 0.014037188 -0.007460885 0.0185907073 0.021930984 +#> 2 0.005263158 1 0 0.02935271 1.0043736 0.9908928 0.9626259 1.025139 0.003365862 0.000000000 0.0143413234 0.015434697 +#> 3 0.010526316 1 0 0.02995027 0.9967094 0.9846668 0.9552792 1.021892 0.000000000 0.000000000 0.0096220212 0.010707439 +#> 4 0.015789474 1 0 0.03010668 0.9897329 0.9789425 0.9481493 1.018672 0.000000000 0.000000000 0.0049333261 0.006364019 +#> 5 0.021052632 1 0 0.03029739 0.9827288 0.9732059 0.9409868 1.015363 0.000000000 0.000000000 0.0001773121 0.002036026 +#> 6 0.026315789 1 0 0.03112461 0.9753368 0.9670620 0.9338617 1.011553 0.000000000 0.000000000 0.0000000000 0.000000000 +#> b9 b10 +#> 1 -0.009900023 0.027401255 +#> 2 -0.007939417 0.022297387 +#> 3 -0.005256688 0.017464767 +#> 4 -0.002393495 0.012713141 +#> 5 0.000000000 0.007969742 +#> 6 0.000000000 0.003303734 ``` @@ -622,7 +615,7 @@ Benchmarking this approach results in: ``` -#> [1] 0.02848101 0.02030015 0.01918411 0.01847601 0.01877809 +#> [1] 0.01474500 0.01400304 0.01196003 0.01131010 0.01107502 ``` So, we have reduced our runtime even more! @@ -640,20 +633,20 @@ C++ library in your packages inst/include folder. It will come to the same parameter estimates: ``` -#> b0 b1 b2 b3 b4 b5 b6 b7 b8 -#> [1,] 0.02734701 1.0129361 0.9991629 0.9705501 1.027728 0.013993181 -0.007491533 0.0186210155 0.021974963 -#> [2,] 0.02939675 1.0043635 0.9908681 0.9626493 1.025035 0.003400965 0.000000000 0.0143089363 0.015388336 -#> [3,] 0.02998680 0.9967117 0.9846504 0.9552960 1.021803 0.000000000 0.000000000 0.0095927088 0.010679371 -#> [4,] 0.03006777 0.9897315 0.9789595 0.9481301 1.018774 0.000000000 0.000000000 0.0049636831 0.006397664 -#> [5,] 0.03032345 0.9827556 0.9731799 0.9409662 1.015441 0.000000000 0.000000000 0.0001374354 0.002100692 -#> [6,] 0.03111085 0.9753325 0.9670615 0.9338506 1.011607 0.000000000 0.000000000 0.0000000000 0.000000000 -#> b9 b10 -#> [1,] -0.009975776 0.027466466 -#> [2,] -0.007860992 0.022231196 -#> [3,] -0.005183552 0.017406513 -#> [4,] -0.002474334 0.012779758 -#> [5,] 0.000000000 0.008033475 -#> [6,] 0.000000000 0.003352431 +#> b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 +#> [1,] 0.02734701 1.0129361 0.9991629 0.9705501 1.027728 0.013993181 -0.007491533 0.0186210155 0.021974963 -0.009975776 +#> [2,] 0.02939675 1.0043635 0.9908681 0.9626493 1.025035 0.003400965 0.000000000 0.0143089363 0.015388336 -0.007860992 +#> [3,] 0.02998680 0.9967117 0.9846504 0.9552960 1.021803 0.000000000 0.000000000 0.0095927088 0.010679371 -0.005183552 +#> [4,] 0.03006777 0.9897315 0.9789595 0.9481301 1.018774 0.000000000 0.000000000 0.0049636831 0.006397664 -0.002474334 +#> [5,] 0.03032345 0.9827556 0.9731799 0.9409662 1.015441 0.000000000 0.000000000 0.0001374354 0.002100692 0.000000000 +#> [6,] 0.03111085 0.9753325 0.9670615 0.9338506 1.011607 0.000000000 0.000000000 0.0000000000 0.000000000 0.000000000 +#> b10 +#> [1,] 0.027466466 +#> [2,] 0.022231196 +#> [3,] 0.017406513 +#> [4,] 0.012779758 +#> [5,] 0.008033475 +#> [6,] 0.003352431 ``` @@ -663,6 +656,6 @@ And the run times are even lower: ``` -#> [1] 0.003325939 0.002278090 0.002166033 0.002274990 0.002366066 +#> [1] 0.002497911 0.001735926 0.001685858 0.001821041 0.001929998 ``` diff --git a/vignettes/Mixed-Penalties.Rmd b/vignettes/Mixed-Penalties.Rmd index fdc857e..de4c6ea 100644 --- a/vignettes/Mixed-Penalties.Rmd +++ b/vignettes/Mixed-Penalties.Rmd @@ -99,14 +99,14 @@ at the `penalty` statement in the resulting object: ```r fitMp@penalty -#> ind60=~x2 ind60=~x3 c2 c3 c4 dem60=~y2 dem60=~y3 dem60=~y4 -#> "none" "none" "lasso" "lasso" "lasso" "none" "none" "none" -#> dem65=~y6 dem65=~y7 c r1 r2 r3 x1~~x1 x2~~x2 -#> "none" "none" "none" "scad" "scad" "scad" "none" "none" -#> x3~~x3 y2~~y2 y3~~y3 y4~~y4 y1~~y1 y5~~y5 y6~~y6 y7~~y7 -#> "none" "none" "none" "none" "none" "none" "none" "none" -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 -#> "none" "none" "none" "none" +#> ind60=~x2 ind60=~x3 c2 c3 c4 dem60=~y2 dem60=~y3 dem60=~y4 dem65=~y6 +#> "none" "none" "lasso" "lasso" "lasso" "none" "none" "none" "none" +#> dem65=~y7 c r1 r2 r3 x1~~x1 x2~~x2 x3~~x3 y2~~y2 +#> "none" "none" "scad" "scad" "scad" "none" "none" "none" "none" +#> y3~~y3 y4~~y4 y1~~y1 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> "none" "none" "none" "none" "none" "none" "none" "none" "none" +#> dem65~~dem65 +#> "none" ``` We can access the best parameters according to the BIC with: @@ -114,30 +114,24 @@ We can access the best parameters according to the BIC with: ```r coef(fitMp, criterion = "BIC") -#> -#> Tuning ||--|| Estimates -#> ---------------------------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> tuningParameterConfiguration ||--|| ind60=~x2 ind60=~x3 c2 c3 c4 dem60=~y2 dem60=~y3 -#> ============================ ||--|| ========== ========== ========== ========== ========== ========== ========== -#> 11.0000 ||--|| 2.1817 1.8188 . . . 1.3540 1.0440 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> dem60=~y4 dem65=~y6 dem65=~y7 c r1 r2 r3 x1~~x1 x2~~x2 x3~~x3 -#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== -#> 1.2995 1.2585 1.2825 1.3098 1.4738 0.4533 0.8644 0.0818 0.1184 0.4673 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------------ -#> y2~~y2 y3~~y3 y4~~y4 y1~~y1 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 -#> ========== ========== ========== ========== ========== ========== ========== ========== ============ -#> 6.4896 5.3399 2.8871 1.9419 2.3901 4.3428 3.5096 2.9403 0.4482 -#> -#> -#> ------------ ------------ -#> dem60~~dem60 dem65~~dem65 -#> ============ ============ -#> 3.8717 0.1149 +#> +#> Tuning ||--|| Estimates +#> ---------------------------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> tuningParameterConfiguration ||--|| ind60=~x2 ind60=~x3 c2 c3 c4 dem60=~y2 dem60=~y3 dem60=~y4 +#> ============================ ||--|| ========== ========== ========== ========== ========== ========== ========== ========== +#> 11.0000 ||--|| 2.1817 1.8188 . . . 1.3540 1.0440 1.2995 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> dem65=~y6 dem65=~y7 c r1 r2 r3 x1~~x1 x2~~x2 x3~~x3 y2~~y2 y3~~y3 +#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 1.2585 1.2825 1.3098 1.4738 0.4533 0.8644 0.0818 0.1184 0.4673 6.4896 5.3399 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ------------ ------------ ------------ +#> y4~~y4 y1~~y1 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 +#> ========== ========== ========== ========== ========== ========== ============ ============ ============ +#> 2.8871 1.9419 2.3901 4.3428 3.5096 2.9403 0.4482 3.8717 0.1149 ``` The `tuningParameterConfiguration` refers to the rows in the lambda, theta, and alpha matrices that resulted in the best fit: @@ -207,30 +201,24 @@ fitMp <- fit(mp) ```r coef(fitMp, criterion = "BIC") -#> -#> Tuning ||--|| Estimates -#> ---------------------------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> tuningParameterConfiguration ||--|| ind60=~x2 ind60=~x3 c2 c3 c4 dem60=~y2 dem60=~y3 -#> ============================ ||--|| ========== ========== ========== ========== ========== ========== ========== -#> 11.0000 ||--|| 2.1818 1.8188 . . . 1.3541 1.0441 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> dem60=~y4 dem65=~y6 dem65=~y7 c r1 r2 r3 x1~~x1 x2~~x2 x3~~x3 -#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== -#> 1.2997 1.2586 1.2825 1.3099 1.4740 0.4532 0.8643 0.0818 0.1184 0.4672 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------------ -#> y2~~y2 y3~~y3 y4~~y4 y1~~y1 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 -#> ========== ========== ========== ========== ========== ========== ========== ========== ============ -#> 6.4892 5.3396 2.8861 1.9420 2.3904 4.3421 3.5090 2.9392 0.4482 -#> -#> -#> ------------ ------------ -#> dem60~~dem60 dem65~~dem65 -#> ============ ============ -#> 3.8710 0.1159 +#> +#> Tuning ||--|| Estimates +#> ---------------------------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> tuningParameterConfiguration ||--|| ind60=~x2 ind60=~x3 c2 c3 c4 dem60=~y2 dem60=~y3 dem60=~y4 +#> ============================ ||--|| ========== ========== ========== ========== ========== ========== ========== ========== +#> 11.0000 ||--|| 2.1818 1.8188 . . . 1.3541 1.0441 1.2997 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> dem65=~y6 dem65=~y7 c r1 r2 r3 x1~~x1 x2~~x2 x3~~x3 y2~~y2 y3~~y3 +#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 1.2586 1.2825 1.3099 1.4740 0.4532 0.8643 0.0818 0.1184 0.4672 6.4892 5.3396 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ------------ ------------ ------------ +#> y4~~y4 y1~~y1 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 +#> ========== ========== ========== ========== ========== ========== ============ ============ ============ +#> 2.8861 1.9420 2.3904 4.3421 3.5090 2.9392 0.4482 3.8710 0.1159 ``` The `tuningParameterConfiguration` refers to the rows in the lambda, theta, and alpha matrices that resulted in the best fit: @@ -274,8 +262,8 @@ getTuningParameterConfiguration(regularizedSEMMixedPenalty = fitMp, Here is a short run-time comparison of ista and glmnet with the lasso-regularized -model from above: Five repetitions using ista took 53.419 seconds, -while glmnet took 2.026 seconds. That is, if you +model from above: Five repetitions using ista took 24.509 seconds, +while glmnet took 1.131 seconds. That is, if you can use glmnet with your model, we recommend that you do. ## Bibliography diff --git a/vignettes/Parameter-transformations.Rmd b/vignettes/Parameter-transformations.Rmd index b32689e..fc1d780 100644 --- a/vignettes/Parameter-transformations.Rmd +++ b/vignettes/Parameter-transformations.Rmd @@ -219,30 +219,30 @@ using information criteria: ```r coef(lassoFit, criterion = "BIC") -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ----------- ----------- -#> lambda alpha ||--|| ind60=~x2 ind60=~x3 a1 b1 c1 dem60~ind60 dem65~ind60 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== =========== =========== -#> 0.2216 1.0000 ||--|| 2.1825 1.8189 1.2110 1.1679 1.2340 1.4534 0.5935 -#> -#> -#> ----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> dem65~dem60 y1~~y5 y2~~y4 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 -#> =========== ========== ========== ========== ========== ========== ========== ========== ========== ========== -#> 0.8659 0.5552 1.5947 0.7807 0.6537 1.5350 0.0820 0.1177 0.4675 1.7929 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------------ ------------ -#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 -#> ========== ========== ========== ========== ========== ========== ========== ============ ============ -#> 7.3843 5.0175 3.4074 2.2857 4.8977 3.5510 3.4511 0.4480 3.9408 -#> -#> ||--|| Transform -#> ------------ ---------- ---------- ---------- ||--|| ---------- ---------- ---------- -#> dem65~~dem65 delta_a2 delta_b2 delta_c2 ||--|| a2 b2 c2 -#> ============ ========== ========== ========== ||--|| ========== ========== ========== -#> 0.2034 . . . ||--|| 1.2110 1.1679 1.2340 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ----------- ----------- ----------- ---------- +#> lambda alpha ||--|| ind60=~x2 ind60=~x3 a1 b1 c1 dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== =========== =========== =========== ========== +#> 0.2216 1.0000 ||--|| 2.1825 1.8189 1.2110 1.1679 1.2340 1.4534 0.5935 0.8659 0.5552 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> y2~~y4 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 +#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 1.5947 0.7807 0.6537 1.5350 0.0820 0.1177 0.4675 1.7929 7.3843 5.0175 3.4074 +#> +#> ||--|| +#> ---------- ---------- ---------- ---------- ------------ ------------ ------------ ---------- ---------- ---------- ||--|| +#> y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 delta_a2 delta_b2 delta_c2 ||--|| +#> ========== ========== ========== ========== ============ ============ ============ ========== ========== ========== ||--|| +#> 2.2857 4.8977 3.5510 3.4511 0.4480 3.9408 0.2034 . . . ||--|| +#> +#> Transform +#> ---------- ---------- ---------- +#> a2 b2 c2 +#> ========== ========== ========== +#> 1.2110 1.1679 1.2340 ``` Note that all differences have been zeroed -- that is, a model with full measurement @@ -385,34 +385,27 @@ The data looks as follows: ```r head(data) -#> y1_t1 y2_t1 y3_t1 y1_t2 y2_t2 y3_t2 y1_t3 y2_t3 y3_t3 -#> [1,] 0.2627294 -0.07745288 -0.06014729 1.1052896 -0.5125258 0.1352253 0.6775861 0.48382200 0.5163607 -#> [2,] -0.5795123 0.27989039 -1.76295329 -1.1646535 0.0607009 -0.7770722 -0.4571539 -0.30575453 0.7552239 -#> [3,] 0.5746526 0.67203557 1.30772224 0.5620735 1.3873269 1.2415508 0.8255529 -0.09010965 1.5877450 -#> [4,] 1.6376973 1.30880439 1.35159277 0.4681794 1.5370870 0.6961462 0.1872212 -1.57377418 1.1081811 -#> [5,] 0.4995286 -0.23722728 0.61244148 0.3469646 0.3052115 0.2639443 1.3768225 0.02394048 1.1573912 -#> [6,] -0.9766373 0.94700260 -0.59421930 -0.3197840 0.8221090 -0.6808501 -0.3434095 -0.90348981 -1.1760506 -#> y1_t4 y2_t4 y3_t4 y1_t5 y2_t5 y3_t5 y1_t6 y2_t6 y3_t6 -#> [1,] 0.06936298 1.6842420 0.5604284 -1.6171372 -1.5100658 -2.1212448 -0.9014921 -0.9732405 -0.89209997 -#> [2,] 0.06618071 -1.5803092 0.1672394 0.1045129 0.3483139 -0.3669688 -0.6347357 -1.1095700 0.01821975 -#> [3,] 0.75756487 1.3579232 -0.1379793 -1.1968405 -0.3623344 -0.8732056 -0.8290071 -0.3555817 -0.98268524 -#> [4,] -0.50832183 1.1968847 -1.6473363 0.4423361 -1.0372594 -0.8153181 -1.6003121 -2.1169045 -0.46541710 -#> [5,] 1.30350064 0.3049751 -0.0146624 1.3351806 0.1229075 0.8274846 0.4508487 0.1328104 0.65883759 -#> [6,] -1.72146444 -1.2828716 -0.4379788 1.0261730 0.1686185 0.6083559 -0.7427341 0.4514472 -1.15435404 -#> y1_t7 y2_t7 y3_t7 y1_t8 y2_t8 y3_t8 y1_t9 y2_t9 y3_t9 -#> [1,] -1.4697023 -0.9088437 -2.1031570 0.9915954 0.26021125 1.2988304 1.27416201 1.2010039 1.1426722 -#> [2,] 0.3845160 1.0140172 1.8585956 -1.1280623 0.62646559 -0.8323908 -0.41574193 -0.6890046 -1.4714503 -#> [3,] -1.8270782 -0.2335533 -1.6441484 -0.2693417 0.09757043 0.5752644 0.37098052 -0.3486185 -0.4273464 -#> [4,] 1.3226249 1.7154104 1.8113749 0.3459617 0.29614603 0.2463911 0.55566216 1.2444869 0.1788493 -#> [5,] -0.3915346 -0.5549143 -0.2476716 -1.3659303 1.14721685 -0.1968243 -1.58477183 -1.8546910 0.7432728 -#> [6,] 1.7453347 1.2488910 0.6649188 -0.6131110 -0.27533871 0.1895920 0.02486185 -0.5151950 -0.2018260 -#> y1_t10 y2_t10 y3_t10 -#> [1,] 0.04668192 -0.24123204 -0.4421784 -#> [2,] -0.01788118 0.02058262 0.3191417 -#> [3,] -0.87844796 -1.36443913 -1.0240934 -#> [4,] 0.81351163 -0.39685221 3.3330365 -#> [5,] 0.89776939 1.05624257 0.3635211 -#> [6,] 1.13097234 0.74034559 2.5292256 +#> y1_t1 y2_t1 y3_t1 y1_t2 y2_t2 y3_t2 y1_t3 y2_t3 y3_t3 y1_t4 +#> [1,] 0.2627294 -0.07745288 -0.06014729 1.1052896 -0.5125258 0.1352253 0.6775861 0.48382200 0.5163607 0.06936298 +#> [2,] -0.5795123 0.27989039 -1.76295329 -1.1646535 0.0607009 -0.7770722 -0.4571539 -0.30575453 0.7552239 0.06618071 +#> [3,] 0.5746526 0.67203557 1.30772224 0.5620735 1.3873269 1.2415508 0.8255529 -0.09010965 1.5877450 0.75756487 +#> [4,] 1.6376973 1.30880439 1.35159277 0.4681794 1.5370870 0.6961462 0.1872212 -1.57377418 1.1081811 -0.50832183 +#> [5,] 0.4995286 -0.23722728 0.61244148 0.3469646 0.3052115 0.2639443 1.3768225 0.02394048 1.1573912 1.30350064 +#> [6,] -0.9766373 0.94700260 -0.59421930 -0.3197840 0.8221090 -0.6808501 -0.3434095 -0.90348981 -1.1760506 -1.72146444 +#> y2_t4 y3_t4 y1_t5 y2_t5 y3_t5 y1_t6 y2_t6 y3_t6 y1_t7 y2_t7 y3_t7 +#> [1,] 1.6842420 0.5604284 -1.6171372 -1.5100658 -2.1212448 -0.9014921 -0.9732405 -0.89209997 -1.4697023 -0.9088437 -2.1031570 +#> [2,] -1.5803092 0.1672394 0.1045129 0.3483139 -0.3669688 -0.6347357 -1.1095700 0.01821975 0.3845160 1.0140172 1.8585956 +#> [3,] 1.3579232 -0.1379793 -1.1968405 -0.3623344 -0.8732056 -0.8290071 -0.3555817 -0.98268524 -1.8270782 -0.2335533 -1.6441484 +#> [4,] 1.1968847 -1.6473363 0.4423361 -1.0372594 -0.8153181 -1.6003121 -2.1169045 -0.46541710 1.3226249 1.7154104 1.8113749 +#> [5,] 0.3049751 -0.0146624 1.3351806 0.1229075 0.8274846 0.4508487 0.1328104 0.65883759 -0.3915346 -0.5549143 -0.2476716 +#> [6,] -1.2828716 -0.4379788 1.0261730 0.1686185 0.6083559 -0.7427341 0.4514472 -1.15435404 1.7453347 1.2488910 0.6649188 +#> y1_t8 y2_t8 y3_t8 y1_t9 y2_t9 y3_t9 y1_t10 y2_t10 y3_t10 +#> [1,] 0.9915954 0.26021125 1.2988304 1.27416201 1.2010039 1.1426722 0.04668192 -0.24123204 -0.4421784 +#> [2,] -1.1280623 0.62646559 -0.8323908 -0.41574193 -0.6890046 -1.4714503 -0.01788118 0.02058262 0.3191417 +#> [3,] -0.2693417 0.09757043 0.5752644 0.37098052 -0.3486185 -0.4273464 -0.87844796 -1.36443913 -1.0240934 +#> [4,] 0.3459617 0.29614603 0.2463911 0.55566216 1.2444869 0.1788493 0.81351163 -0.39685221 3.3330365 +#> [5,] -1.3659303 1.14721685 -0.1968243 -1.58477183 -1.8546910 0.7432728 0.89776939 1.05624257 0.3635211 +#> [6,] -0.6131110 -0.27533871 0.1895920 0.02486185 -0.5151950 -0.2018260 1.13097234 0.74034559 2.5292256 ``` @@ -494,26 +487,24 @@ lavaanFit <- sem(model = lavaanSyntax, orthogonal.x = TRUE, missing = "ml") coef(lavaanFit) -#> a1 a2 a3 a4 a5 a6 a7 a8 a9 eta1~~eta1 -#> 0.484 0.535 0.531 0.135 0.124 0.040 0.173 0.296 0.185 1.108 -#> v v v v v v v v v l2 -#> 0.804 0.804 0.804 0.804 0.804 0.804 0.804 0.804 0.804 0.567 -#> l3 mvar1 mvar2 mvar3 l2 l3 mvar1 mvar2 mvar3 l2 -#> 0.672 0.049 0.744 0.652 0.567 0.672 0.049 0.744 0.652 0.567 -#> l3 mvar1 mvar2 mvar3 l2 l3 mvar1 mvar2 mvar3 l2 -#> 0.672 0.049 0.744 0.652 0.567 0.672 0.049 0.744 0.652 0.567 -#> l3 mvar1 mvar2 mvar3 l2 l3 mvar1 mvar2 mvar3 l2 -#> 0.672 0.049 0.744 0.652 0.567 0.672 0.049 0.744 0.652 0.567 -#> l3 mvar1 mvar2 mvar3 l2 l3 mvar1 mvar2 mvar3 l2 -#> 0.672 0.049 0.744 0.652 0.567 0.672 0.049 0.744 0.652 0.567 -#> l3 mvar1 mvar2 mvar3 l2 l3 mvar1 mvar2 mvar3 y1_t1~1 -#> 0.672 0.049 0.744 0.652 0.567 0.672 0.049 0.744 0.652 -0.155 -#> y2_t1~1 y3_t1~1 y1_t2~1 y2_t2~1 y3_t2~1 y1_t3~1 y2_t3~1 y3_t3~1 y1_t4~1 y2_t4~1 -#> -0.088 -0.175 -0.157 -0.049 -0.085 -0.150 -0.030 -0.157 -0.095 -0.022 -#> y3_t4~1 y1_t5~1 y2_t5~1 y3_t5~1 y1_t6~1 y2_t6~1 y3_t6~1 y1_t7~1 y2_t7~1 y3_t7~1 -#> -0.080 0.037 -0.023 0.058 0.050 0.065 -0.002 -0.047 0.068 -0.038 -#> y1_t8~1 y2_t8~1 y3_t8~1 y1_t9~1 y2_t9~1 y3_t9~1 y1_t10~1 y2_t10~1 y3_t10~1 -#> -0.022 -0.028 -0.048 -0.077 -0.058 -0.041 0.013 -0.012 -0.004 +#> a1 a2 a3 a4 a5 a6 a7 a8 a9 eta1~~eta1 v +#> 0.484 0.535 0.531 0.135 0.124 0.040 0.173 0.296 0.185 1.108 0.804 +#> v v v v v v v v l2 l3 mvar1 +#> 0.804 0.804 0.804 0.804 0.804 0.804 0.804 0.804 0.567 0.672 0.049 +#> mvar2 mvar3 l2 l3 mvar1 mvar2 mvar3 l2 l3 mvar1 mvar2 +#> 0.744 0.652 0.567 0.672 0.049 0.744 0.652 0.567 0.672 0.049 0.744 +#> mvar3 l2 l3 mvar1 mvar2 mvar3 l2 l3 mvar1 mvar2 mvar3 +#> 0.652 0.567 0.672 0.049 0.744 0.652 0.567 0.672 0.049 0.744 0.652 +#> l2 l3 mvar1 mvar2 mvar3 l2 l3 mvar1 mvar2 mvar3 l2 +#> 0.567 0.672 0.049 0.744 0.652 0.567 0.672 0.049 0.744 0.652 0.567 +#> l3 mvar1 mvar2 mvar3 l2 l3 mvar1 mvar2 mvar3 l2 l3 +#> 0.672 0.049 0.744 0.652 0.567 0.672 0.049 0.744 0.652 0.567 0.672 +#> mvar1 mvar2 mvar3 y1_t1~1 y2_t1~1 y3_t1~1 y1_t2~1 y2_t2~1 y3_t2~1 y1_t3~1 y2_t3~1 +#> 0.049 0.744 0.652 -0.155 -0.088 -0.175 -0.157 -0.049 -0.085 -0.150 -0.030 +#> y3_t3~1 y1_t4~1 y2_t4~1 y3_t4~1 y1_t5~1 y2_t5~1 y3_t5~1 y1_t6~1 y2_t6~1 y3_t6~1 y1_t7~1 +#> -0.157 -0.095 -0.022 -0.080 0.037 -0.023 0.058 0.050 0.065 -0.002 -0.047 +#> y2_t7~1 y3_t7~1 y1_t8~1 y2_t8~1 y3_t8~1 y1_t9~1 y2_t9~1 y3_t9~1 y1_t10~1 y2_t10~1 y3_t10~1 +#> 0.068 -0.038 -0.022 -0.028 -0.048 -0.077 -0.058 -0.041 0.013 -0.012 -0.004 ``` Note that no constraints on autoregressive effects are implemented -- each @@ -559,42 +550,42 @@ Extracting the best fitting model: ```r coef(lassoFit, criterion = "BIC") -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> lambda alpha ||--|| a1 eta1~~eta1 v l2 l3 mvar1 mvar2 mvar3 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== -#> 0.2790 1.0000 ||--|| 0.4665 1.1055 0.8051 0.5696 0.6754 0.0533 0.7428 0.6500 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> y1_t1~1 y2_t1~1 y3_t1~1 y1_t2~1 y2_t2~1 y3_t2~1 y1_t3~1 y2_t3~1 y3_t3~1 y1_t4~1 -#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== -#> -0.1545 -0.0878 -0.1745 -0.1565 -0.0492 -0.0849 -0.1499 -0.0302 -0.1572 -0.0952 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> y2_t4~1 y3_t4~1 y1_t5~1 y2_t5~1 y3_t5~1 y1_t6~1 y2_t6~1 y3_t6~1 y1_t7~1 y2_t7~1 -#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== -#> -0.0224 -0.0798 0.0374 -0.0231 0.0576 0.0502 0.0651 -0.0020 -0.0466 0.0678 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> y3_t7~1 y1_t8~1 y2_t8~1 y3_t8~1 y1_t9~1 y2_t9~1 y3_t9~1 y1_t10~1 y2_t10~1 y3_t10~1 -#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== -#> -0.0383 -0.0221 -0.0279 -0.0476 -0.0775 -0.0579 -0.0407 0.0131 -0.0118 -0.0043 -#> -#> ||--|| Transform -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ||--|| ---------- -#> delta2 delta3 delta4 delta5 delta6 delta7 delta8 delta9 ||--|| a2 -#> ========== ========== ========== ========== ========== ========== ========== ========== ||--|| ========== -#> . . -0.2807 . . . . . ||--|| 0.4665 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> a3 a4 a5 a6 a7 a8 a9 -#> ========== ========== ========== ========== ========== ========== ========== -#> 0.4665 0.1859 0.1859 0.1859 0.1859 0.1859 0.1859 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> lambda alpha ||--|| a1 eta1~~eta1 v l2 l3 mvar1 mvar2 mvar3 y1_t1~1 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 0.2790 1.0000 ||--|| 0.4665 1.1055 0.8051 0.5696 0.6754 0.0533 0.7428 0.6500 -0.1545 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> y2_t1~1 y3_t1~1 y1_t2~1 y2_t2~1 y3_t2~1 y1_t3~1 y2_t3~1 y3_t3~1 y1_t4~1 y2_t4~1 y3_t4~1 +#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> -0.0878 -0.1745 -0.1565 -0.0492 -0.0849 -0.1499 -0.0302 -0.1572 -0.0952 -0.0224 -0.0798 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> y1_t5~1 y2_t5~1 y3_t5~1 y1_t6~1 y2_t6~1 y3_t6~1 y1_t7~1 y2_t7~1 y3_t7~1 y1_t8~1 y2_t8~1 +#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 0.0374 -0.0231 0.0576 0.0502 0.0651 -0.0020 -0.0466 0.0678 -0.0383 -0.0221 -0.0279 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> y3_t8~1 y1_t9~1 y2_t9~1 y3_t9~1 y1_t10~1 y2_t10~1 y3_t10~1 delta2 delta3 delta4 delta5 +#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> -0.0476 -0.0775 -0.0579 -0.0407 0.0131 -0.0118 -0.0043 . . -0.2807 . +#> +#> ||--|| Transform +#> ---------- ---------- ---------- ---------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- +#> delta6 delta7 delta8 delta9 ||--|| a2 a3 a4 a5 a6 a7 +#> ========== ========== ========== ========== ||--|| ========== ========== ========== ========== ========== ========== +#> . . . . ||--|| 0.4665 0.4665 0.1859 0.1859 0.1859 0.1859 +#> +#> +#> ---------- ---------- +#> a8 a9 +#> ========== ========== +#> 0.1859 0.1859 ``` The true autoregressive effects are given by @@ -896,10 +887,10 @@ lavaanFit <- sem(model = lavaanSyntax, orthogonal.x = TRUE, missing = "ml") getLavaanParameters(lavaanFit) -#> a eta0~~eta0 v eta0~1 l2 l3 mvar1 mvar2 -#> 0.592770935 0.873870399 0.387065602 -0.091482670 0.578004929 0.745298413 0.127999058 0.750360430 -#> mvar3 mMean1 mMean2 mMean3 -#> 0.577566347 -0.026015718 0.005858067 -0.027032832 +#> a eta0~~eta0 v eta0~1 l2 l3 mvar1 mvar2 mvar3 +#> 0.592770935 0.873870399 0.387065602 -0.091482670 0.578004929 0.745298413 0.127999058 0.750360430 0.577566347 +#> mMean1 mMean2 mMean3 +#> -0.026015718 0.005858067 -0.027032832 ``` Now, we define the transformations for the latent variables to turn our model @@ -937,18 +928,18 @@ Let's have a look at the parameter estimates: ```r coef(lessSEMFit) -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> lambda alpha ||--|| eta0~~eta0 eta0~1 l2 l3 mvar1 mvar2 mvar3 mMean1 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== -#> 0.0000 0.0000 ||--|| 0.8736 -0.0902 0.5780 0.7453 0.1280 0.7504 0.5775 -0.0267 -#> -#> ||--|| Transform -#> ---------- ---------- ---------- ---------- ||--|| ---------- ---------- -#> mMean2 mMean3 ctA ctV ||--|| a v -#> ========== ========== ========== ========== ||--|| ========== ========== -#> 0.0054 -0.0276 -0.5228 0.7899 ||--|| 0.5928 0.3870 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> lambda alpha ||--|| eta0~~eta0 eta0~1 l2 l3 mvar1 mvar2 mvar3 mMean1 mMean2 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 0.0000 0.0000 ||--|| 0.8736 -0.0902 0.5780 0.7453 0.1280 0.7504 0.5775 -0.0267 0.0054 +#> +#> ||--|| Transform +#> ---------- ---------- ---------- ||--|| ---------- ---------- +#> mMean3 ctA ctV ||--|| a v +#> ========== ========== ========== ||--|| ========== ========== +#> -0.0276 -0.5228 0.7899 ||--|| 0.5928 0.3870 ``` For comparison, we will run the same model with **ctsem**: diff --git a/vignettes/The-Structural-Equation-Model.Rmd b/vignettes/The-Structural-Equation-Model.Rmd index 6cc2246..d427723 100644 --- a/vignettes/The-Structural-Equation-Model.Rmd +++ b/vignettes/The-Structural-Equation-Model.Rmd @@ -70,16 +70,16 @@ mySEM <- lessSEM:::.SEMFromLavaan(lavaanModel = lavaanModel) show(mySEM) #> Internal C++ model representation of lessSEM #> Parameters: -#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 -#> 2.1796566 1.8182100 1.1907820 1.1745407 1.2509789 1.4713302 0.6004746 0.8650430 -#> y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 -#> 0.5825389 1.4402477 2.1829448 0.7115901 0.3627964 1.3717741 0.0813878 0.1204271 -#> x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> 0.4666596 1.8546417 7.5813926 4.9556766 3.2245521 2.3130404 4.9681408 3.5600367 -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 -#> 3.3076854 0.4485989 3.8753039 0.1644633 5.0543838 4.7921946 3.5576898 5.4646667 -#> y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 -#> 4.2564429 6.5631103 4.4525330 5.1362519 2.9780741 6.1962639 4.0433897 +#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> 2.1796566 1.8182100 1.1907820 1.1745407 1.2509789 1.4713302 0.6004746 0.8650430 0.5825389 +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 +#> 1.4402477 2.1829448 0.7115901 0.3627964 1.3717741 0.0813878 0.1204271 0.4666596 1.8546417 +#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> 7.5813926 4.9556766 3.2245521 2.3130404 4.9681408 3.5600367 3.3076854 0.4485989 3.8753039 +#> dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 y5~1 +#> 0.1644633 5.0543838 4.7921946 3.5576898 5.4646667 4.2564429 6.5631103 4.4525330 5.1362519 +#> y6~1 y7~1 y8~1 +#> 2.9780741 6.1962639 4.0433897 #> #> Objective value: 3097.6361581071 ``` @@ -152,16 +152,16 @@ The parameters of the model can be accessed with the `lessSEM:::.getParameters` ```r (myParameters <- lessSEM:::.getParameters(mySEM)) -#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 -#> 2.1796566 1.8182100 1.1907820 1.1745407 1.2509789 1.4713302 0.6004746 0.8650430 -#> y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 -#> 0.5825389 1.4402477 2.1829448 0.7115901 0.3627964 1.3717741 0.0813878 0.1204271 -#> x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> 0.4666596 1.8546417 7.5813926 4.9556766 3.2245521 2.3130404 4.9681408 3.5600367 -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 -#> 3.3076854 0.4485989 3.8753039 0.1644633 5.0543838 4.7921946 3.5576898 5.4646667 -#> y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 -#> 4.2564429 6.5631103 4.4525330 5.1362519 2.9780741 6.1962639 4.0433897 +#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> 2.1796566 1.8182100 1.1907820 1.1745407 1.2509789 1.4713302 0.6004746 0.8650430 0.5825389 +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 +#> 1.4402477 2.1829448 0.7115901 0.3627964 1.3717741 0.0813878 0.1204271 0.4666596 1.8546417 +#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> 7.5813926 4.9556766 3.2245521 2.3130404 4.9681408 3.5600367 3.3076854 0.4485989 3.8753039 +#> dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 y5~1 +#> 0.1644633 5.0543838 4.7921946 3.5576898 5.4646667 4.2564429 6.5631103 4.4525330 5.1362519 +#> y6~1 y7~1 y8~1 +#> 2.9780741 6.1962639 4.0433897 ``` The naming is identical to that of the lavaanModel. By default, the parameters are @@ -225,16 +225,16 @@ positive. You can access the raw values as follows: ```r lessSEM:::.getParameters(mySEM, raw = TRUE) -#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 -#> 2.1796566 1.8182100 1.1907820 1.1745407 1.2509789 1.4713302 0.6004746 0.8650430 -#> y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 -#> 0.5825389 1.4402477 2.1829448 0.7115901 0.3627964 1.3717741 -2.5085299 -2.1167106 -#> x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> -0.7621551 0.6176915 2.0256969 1.6005337 1.1707941 0.8385629 1.6030457 1.2697708 -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 -#> 1.1962487 -0.8016262 1.3546241 -1.8050678 5.0543838 4.7921946 3.5576898 5.4646667 -#> y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 -#> 4.2564429 6.5631103 4.4525330 5.1362519 2.9780741 6.1962639 4.0433897 +#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> 2.1796566 1.8182100 1.1907820 1.1745407 1.2509789 1.4713302 0.6004746 0.8650430 0.5825389 +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 +#> 1.4402477 2.1829448 0.7115901 0.3627964 1.3717741 -2.5085299 -2.1167106 -0.7621551 0.6176915 +#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> 2.0256969 1.6005337 1.1707941 0.8385629 1.6030457 1.2697708 1.1962487 -0.8016262 1.3546241 +#> dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 y5~1 +#> -1.8050678 5.0543838 4.7921946 3.5576898 5.4646667 4.2564429 6.5631103 4.4525330 5.1362519 +#> y6~1 y7~1 y8~1 +#> 2.9780741 6.1962639 4.0433897 ``` Note that the raw value for `ind60~~ind60` is negative while the transformed value is positive. @@ -278,16 +278,16 @@ Let's check the parameters: ```r lessSEM:::.getParameters(mySEM) -#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 -#> 2.1796566 1.8182100 1.0000000 1.1745407 1.2509789 1.4713302 0.6004746 0.8650430 -#> y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 -#> 0.5825389 1.4402477 2.1829448 0.7115901 0.3627964 1.3717741 0.0813878 0.1204271 -#> x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> 0.4666596 1.8546417 7.5813926 4.9556766 3.2245521 2.3130404 4.9681408 3.5600367 -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 -#> 3.3076854 0.4485989 3.8753039 0.1644633 5.0543838 4.7921946 3.5576898 5.4646667 -#> y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 -#> 4.2564429 6.5631103 4.4525330 5.1362519 2.9780741 6.1962639 4.0433897 +#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> 2.1796566 1.8182100 1.0000000 1.1745407 1.2509789 1.4713302 0.6004746 0.8650430 0.5825389 +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 +#> 1.4402477 2.1829448 0.7115901 0.3627964 1.3717741 0.0813878 0.1204271 0.4666596 1.8546417 +#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> 7.5813926 4.9556766 3.2245521 2.3130404 4.9681408 3.5600367 3.3076854 0.4485989 3.8753039 +#> dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 y5~1 +#> 0.1644633 5.0543838 4.7921946 3.5576898 5.4646667 4.2564429 6.5631103 4.4525330 5.1362519 +#> y6~1 y7~1 y8~1 +#> 2.9780741 6.1962639 4.0433897 ``` Note that `a` now has the value `1`. @@ -318,32 +318,32 @@ can be computed for the transformed parameters ```r lessSEM:::.getGradients(mySEM, raw = FALSE) -#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 -#> 0.361097622 0.105564095 -32.837359814 2.453232158 17.076222049 -0.450648533 -1.078272542 -5.357920036 -#> y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 -#> 0.004650747 -0.157923486 -0.567076177 0.161163293 0.266869495 -0.271533827 0.230054158 0.306685898 -#> x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> -0.093753843 -0.015516535 -0.343353554 0.099359383 0.137753880 0.131454473 -0.330083693 0.073331567 -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 -#> 0.148964628 -0.103960291 -0.252921392 -1.955349241 0.000000000 0.000000000 0.000000000 0.000000000 -#> y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 -#> 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 +#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> 0.361097622 0.105564095 -32.837359814 2.453232158 17.076222049 -0.450648533 -1.078272542 -5.357920036 0.004650747 +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 +#> -0.157923486 -0.567076177 0.161163293 0.266869495 -0.271533827 0.230054158 0.306685898 -0.093753843 -0.015516535 +#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> -0.343353554 0.099359383 0.137753880 0.131454473 -0.330083693 0.073331567 0.148964628 -0.103960291 -0.252921392 +#> dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 y5~1 +#> -1.955349241 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 +#> y6~1 y7~1 y8~1 +#> 0.000000000 0.000000000 0.000000000 ``` or for the raw parameters ```r lessSEM:::.getGradients(mySEM, raw = TRUE) -#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 -#> 0.361097622 0.105564095 -32.837359814 2.453232158 17.076222049 -0.450648533 -1.078272542 -5.357920036 -#> y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 -#> 0.004650747 -0.157923486 -0.567076177 0.161163293 0.266869495 -0.271533827 0.018723602 0.036933297 -#> x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> -0.043751134 -0.028777612 -2.603098086 0.492392971 0.444194569 0.304059508 -1.639902248 0.261063066 -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 -#> 0.492728130 -0.046636468 -0.980147244 -0.321583201 0.000000000 0.000000000 0.000000000 0.000000000 -#> y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 -#> 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 +#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> 0.361097622 0.105564095 -32.837359814 2.453232158 17.076222049 -0.450648533 -1.078272542 -5.357920036 0.004650747 +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 +#> -0.157923486 -0.567076177 0.161163293 0.266869495 -0.271533827 0.018723602 0.036933297 -0.043751134 -0.028777612 +#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> -2.603098086 0.492392971 0.444194569 0.304059508 -1.639902248 0.261063066 0.492728130 -0.046636468 -0.980147244 +#> dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 y5~1 +#> -0.321583201 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 +#> y6~1 y7~1 y8~1 +#> 0.000000000 0.000000000 0.000000000 ``` ## Computing the Hessian @@ -416,16 +416,16 @@ We can use this in optim as follows: par <- lessSEM:::.getParameters(mySEM, raw = TRUE) # important: Use raw = TRUE! print(par) -#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 -#> 2.1796566 1.8182100 1.0000000 1.1745407 1.2509789 1.4713302 0.6004746 0.8650430 -#> y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 -#> 0.5825389 1.4402477 2.1829448 0.7115901 0.3627964 1.3717741 -2.5085299 -2.1167106 -#> x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> -0.7621551 0.6176915 2.0256969 1.6005337 1.1707941 0.8385629 1.6030457 1.2697708 -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 -#> 1.1962487 -0.8016262 1.3546241 -1.8050678 5.0543838 4.7921946 3.5576898 5.4646667 -#> y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 -#> 4.2564429 6.5631103 4.4525330 5.1362519 2.9780741 6.1962639 4.0433897 +#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> 2.1796566 1.8182100 1.0000000 1.1745407 1.2509789 1.4713302 0.6004746 0.8650430 0.5825389 +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 +#> 1.4402477 2.1829448 0.7115901 0.3627964 1.3717741 -2.5085299 -2.1167106 -0.7621551 0.6176915 +#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> 2.0256969 1.6005337 1.1707941 0.8385629 1.6030457 1.2697708 1.1962487 -0.8016262 1.3546241 +#> dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 y5~1 +#> -1.8050678 5.0543838 4.7921946 3.5576898 5.4646667 4.2564429 6.5631103 4.4525330 5.1362519 +#> y6~1 y7~1 y8~1 +#> 2.9780741 6.1962639 4.0433897 opt <- optim(par = par, fn = lessSEM:::.fitFunction, # use the fitting function wrapper @@ -435,16 +435,16 @@ opt <- optim(par = par, method = "BFGS" # use the BFGS optimizer ) print(opt$par) -#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 -#> 2.1791276 1.8180458 1.1909397 1.1740909 1.2511328 1.4725867 0.6007137 0.8649836 -#> y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 -#> 0.5817910 1.4336940 2.1828828 0.7229781 0.3605874 1.3774602 -2.5093044 -2.1126718 -#> x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> -0.7633056 0.6178333 2.0246995 1.6022529 1.1690474 0.8385775 1.6039528 1.2711405 -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 -#> 1.1971146 -0.8013687 1.3542390 -1.8057174 5.0543838 4.7921946 3.5576898 5.4646667 -#> y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 -#> 4.2564429 6.5631103 4.4525330 5.1362519 2.9780741 6.1962639 4.0433897 +#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> 2.1791276 1.8180458 1.1909397 1.1740909 1.2511328 1.4725867 0.6007137 0.8649836 0.5817910 +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 +#> 1.4336940 2.1828828 0.7229781 0.3605874 1.3774602 -2.5093044 -2.1126718 -0.7633056 0.6178333 +#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> 2.0246995 1.6022529 1.1690474 0.8385775 1.6039528 1.2711405 1.1971146 -0.8013687 1.3542390 +#> dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 y5~1 +#> -1.8057174 5.0543838 4.7921946 3.5576898 5.4646667 4.2564429 6.5631103 4.4525330 5.1362519 +#> y6~1 y7~1 y8~1 +#> 2.9780741 6.1962639 4.0433897 ``` Note that the parameter `a` is now back at the maximum likelihood estimate from before. @@ -458,16 +458,16 @@ mySEM <- lessSEM:::.setParameters(SEM = mySEM, # the model values = opt$par, # values of the parameters raw = TRUE) print(lessSEM:::.getParameters(mySEM, raw = FALSE)) -#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 -#> 2.17912764 1.81804575 1.19093968 1.17409087 1.25113276 1.47258673 0.60071368 0.86498364 -#> y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 -#> 0.58179103 1.43369401 2.18288278 0.72297808 0.36058737 1.37746019 0.08132479 0.12091448 -#> x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> 0.46612308 1.85490471 7.57383439 4.96420359 3.21892497 2.31307420 4.97264951 3.56491599 -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 -#> 3.31055101 0.44871438 3.87381195 0.16435650 5.05438384 4.79219463 3.55768979 5.46466667 -#> y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 -#> 4.25644288 6.56311025 4.45253304 5.13625192 2.97807408 6.19626389 4.04338968 +#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> 2.17912764 1.81804575 1.19093968 1.17409087 1.25113276 1.47258673 0.60071368 0.86498364 0.58179103 +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 +#> 1.43369401 2.18288278 0.72297808 0.36058737 1.37746019 0.08132479 0.12091448 0.46612308 1.85490471 +#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> 7.57383439 4.96420359 3.21892497 2.31307420 4.97264951 3.56491599 3.31055101 0.44871438 3.87381195 +#> dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 y5~1 +#> 0.16435650 5.05438384 4.79219463 3.55768979 5.46466667 4.25644288 6.56311025 4.45253304 5.13625192 +#> y6~1 y7~1 y8~1 +#> 2.97807408 6.19626389 4.04338968 ``` Compare those to the parameter estimates from **lavaan**: @@ -475,14 +475,14 @@ Compare those to the parameter estimates from **lavaan**: ```r coef(lavaanModel) -#> ind60=~x2 ind60=~x3 a b c a b c -#> 2.180 1.818 1.191 1.175 1.251 1.191 1.175 1.251 -#> dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 -#> 1.471 0.600 0.865 0.583 1.440 2.183 0.712 0.363 -#> y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 -#> 1.372 0.081 0.120 0.467 1.855 7.581 4.956 3.225 -#> y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 -#> 2.313 4.968 3.560 3.308 0.449 3.875 0.164 +#> ind60=~x2 ind60=~x3 a b c a b c dem60~ind60 +#> 2.180 1.818 1.191 1.175 1.251 1.191 1.175 1.251 1.471 +#> dem65~ind60 dem65~dem60 y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 +#> 0.600 0.865 0.583 1.440 2.183 0.712 0.363 1.372 0.081 +#> x2~~x2 x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 +#> 0.120 0.467 1.855 7.581 4.956 3.225 2.313 4.968 3.560 +#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 +#> 3.308 0.449 3.875 0.164 ``` @@ -491,16 +491,16 @@ Finally, we can compute the standard errors: ```r lessSEM:::.standardErrors(SEM = mySEM, raw = FALSE) -#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 -#> 0.13885220 0.15204330 0.14166120 0.11987057 0.12295637 0.39139697 0.23828914 0.07567860 -#> y1~~y5 y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 -#> 0.36462027 0.68977247 0.73096919 0.62119517 0.46062832 0.57969390 0.01968652 0.06991196 -#> x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> 0.08897395 0.45717112 1.34332170 0.96373267 0.74092220 0.48364101 0.89600779 0.73922562 -#> y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 x1~1 x2~1 x3~1 y1~1 -#> 0.71425331 0.08675480 0.88802932 0.23331748 0.08406657 0.17326967 0.16121433 0.29892606 -#> y2~1 y3~1 y4~1 y5~1 y6~1 y7~1 y8~1 -#> 0.43891242 0.39404806 0.37957637 0.30446534 0.39247640 0.36442149 0.37545879 +#> ind60=~x2 ind60=~x3 a b c dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> 0.13885220 0.15204330 0.14166120 0.11987057 0.12295637 0.39139697 0.23828914 0.07567860 0.36462027 +#> y2~~y4 y2~~y6 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 +#> 0.68977247 0.73096919 0.62119517 0.46062832 0.57969390 0.01968652 0.06991196 0.08897395 0.45717112 +#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 +#> 1.34332170 0.96373267 0.74092220 0.48364101 0.89600779 0.73922562 0.71425331 0.08675480 0.88802932 +#> dem65~~dem65 x1~1 x2~1 x3~1 y1~1 y2~1 y3~1 y4~1 y5~1 +#> 0.23331748 0.08406657 0.17326967 0.16121433 0.29892606 0.43891242 0.39404806 0.37957637 0.30446534 +#> y6~1 y7~1 y8~1 +#> 0.39247640 0.36442149 0.37545879 ``` Let's compare this to **lavaan** again: diff --git a/vignettes/lessSEM.Rmd b/vignettes/lessSEM.Rmd index 5c59867..e95a412 100644 --- a/vignettes/lessSEM.Rmd +++ b/vignettes/lessSEM.Rmd @@ -165,43 +165,43 @@ The `coef` function gives access to all parameter estimates: ```r coef(fitLasso) -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> lambda alpha ||--|| l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== -#> 0.1034 1.0000 ||--|| 0.7523 0.7536 0.5742 . . . 0.8812 1.1477 -#> 0.0776 1.0000 ||--|| 0.7477 0.7480 0.5720 -0.0104 . . 0.8742 1.1523 -#> 0.0517 1.0000 ||--|| 0.7399 0.7396 0.5688 -0.0266 . -0.0090 0.8631 1.1602 -#> 0.0259 1.0000 ||--|| 0.7301 0.7332 0.5677 -0.0418 . -0.0478 0.8528 1.1706 -#> 0.0000 1.0000 ||--|| 0.7239 0.7319 0.5688 -0.0562 0.0166 -0.0894 0.8491 1.1779 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- -#> y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> ========== ========== ========== ========== ========== -#> 1.9273 1.0804 0.5710 0.9628 1.5320 -#> 1.9331 1.0818 0.5705 0.9628 1.5320 -#> 1.9417 1.0838 0.5697 0.9628 1.5312 -#> 1.9482 1.0841 0.5689 0.9628 1.5282 -#> 1.9496 1.0830 0.5685 0.9626 1.5255 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> lambda alpha ||--|| l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 y3~~y3 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 0.1034 1.0000 ||--|| 0.7523 0.7536 0.5742 . . . 0.8812 1.1477 1.9273 +#> 0.0776 1.0000 ||--|| 0.7477 0.7480 0.5720 -0.0104 . . 0.8742 1.1523 1.9331 +#> 0.0517 1.0000 ||--|| 0.7399 0.7396 0.5688 -0.0266 . -0.0090 0.8631 1.1602 1.9417 +#> 0.0259 1.0000 ||--|| 0.7301 0.7332 0.5677 -0.0418 . -0.0478 0.8528 1.1706 1.9482 +#> 0.0000 1.0000 ||--|| 0.7239 0.7319 0.5688 -0.0562 0.0166 -0.0894 0.8491 1.1779 1.9496 +#> +#> +#> ---------- ---------- ---------- ---------- +#> y4~~y4 y5~~y5 y6~~y6 y7~~y7 +#> ========== ========== ========== ========== +#> 1.0804 0.5710 0.9628 1.5320 +#> 1.0818 0.5705 0.9628 1.5320 +#> 1.0838 0.5697 0.9628 1.5312 +#> 1.0841 0.5689 0.9628 1.5282 +#> 1.0830 0.5685 0.9626 1.5255 ``` If you are only interested in the estimates, use ```r estimates(fitLasso) -#> l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 y3~~y3 y4~~y4 -#> [1,] 0.7522904 0.7536140 0.5742228 0.00000000 0.00000000 0.000000000 0.8812416 1.147737 1.927350 1.080353 -#> [2,] 0.7476862 0.7480112 0.5720039 -0.01042497 0.00000000 0.000000000 0.8741617 1.152318 1.933141 1.081837 -#> [3,] 0.7398991 0.7395932 0.5688300 -0.02660810 0.00000000 -0.008974807 0.8630783 1.160213 1.941722 1.083766 -#> [4,] 0.7301019 0.7331646 0.5677000 -0.04181445 0.00000000 -0.047849804 0.8528254 1.170580 1.948163 1.084065 -#> [5,] 0.7239003 0.7318701 0.5688230 -0.05624391 0.01658325 -0.089365627 0.8491201 1.177878 1.949609 1.082966 -#> y5~~y5 y6~~y6 y7~~y7 -#> [1,] 0.5710041 0.9628056 1.531997 -#> [2,] 0.5704936 0.9628058 1.532015 -#> [3,] 0.5696926 0.9628057 1.531243 -#> [4,] 0.5689390 0.9628055 1.528245 -#> [5,] 0.5684702 0.9625805 1.525473 +#> l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 +#> [1,] 0.7522904 0.7536140 0.5742228 0.00000000 0.00000000 0.000000000 0.8812416 1.147737 1.927350 1.080353 0.5710041 0.9628056 +#> [2,] 0.7476862 0.7480112 0.5720039 -0.01042497 0.00000000 0.000000000 0.8741617 1.152318 1.933141 1.081837 0.5704936 0.9628058 +#> [3,] 0.7398991 0.7395932 0.5688300 -0.02660810 0.00000000 -0.008974807 0.8630783 1.160213 1.941722 1.083766 0.5696926 0.9628057 +#> [4,] 0.7301019 0.7331646 0.5677000 -0.04181445 0.00000000 -0.047849804 0.8528254 1.170580 1.948163 1.084065 0.5689390 0.9628055 +#> [5,] 0.7239003 0.7318701 0.5688230 -0.05624391 0.01658325 -0.089365627 0.8491201 1.177878 1.949609 1.082966 0.5684702 0.9625805 +#> y7~~y7 +#> [1,] 1.531997 +#> [2,] 1.532015 +#> [3,] 1.531243 +#> [4,] 1.528245 +#> [5,] 1.525473 ``` Now, let's assume you also want to try out the scad penalty. In this case, @@ -231,32 +231,32 @@ The parameter estimates can again be accessed with the `coef()` function: ```r coef(fitScad) -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> lambda theta ||--|| l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== -#> 0.0000 2.1000 ||--|| 0.7240 0.7320 0.5689 -0.0562 0.0166 -0.0894 0.8492 1.1778 -#> 0.3333 2.1000 ||--|| 0.7523 0.7535 0.5742 . . . 0.8812 1.1478 -#> 0.6667 2.1000 ||--|| 0.7522 0.7536 0.5742 . . . 0.8812 1.1478 -#> 1.0000 2.1000 ||--|| 0.7522 0.7536 0.5742 . . . 0.8812 1.1478 -#> 0.0000 5.0000 ||--|| 0.7242 0.7323 0.5690 -0.0562 0.0166 -0.0894 0.8495 1.1776 -#> 0.3333 5.0000 ||--|| 0.7522 0.7535 0.5740 . . . 0.8811 1.1479 -#> 0.6667 5.0000 ||--|| 0.7522 0.7535 0.5742 . . . 0.8811 1.1479 -#> 1.0000 5.0000 ||--|| 0.7522 0.7535 0.5742 . . . 0.8811 1.1478 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- -#> y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> ========== ========== ========== ========== ========== -#> 1.9495 1.0829 0.5684 0.9626 1.5255 -#> 1.9274 1.0804 0.5710 0.9628 1.5320 -#> 1.9274 1.0804 0.5710 0.9628 1.5320 -#> 1.9274 1.0804 0.5710 0.9628 1.5320 -#> 1.9493 1.0829 0.5684 0.9626 1.5255 -#> 1.9275 1.0805 0.5711 0.9628 1.5320 -#> 1.9274 1.0804 0.5710 0.9628 1.5320 -#> 1.9275 1.0804 0.5710 0.9628 1.5320 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> lambda theta ||--|| l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 y3~~y3 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 0.0000 2.1000 ||--|| 0.7240 0.7320 0.5689 -0.0562 0.0166 -0.0894 0.8492 1.1778 1.9495 +#> 0.3333 2.1000 ||--|| 0.7523 0.7535 0.5742 . . . 0.8812 1.1478 1.9274 +#> 0.6667 2.1000 ||--|| 0.7522 0.7536 0.5742 . . . 0.8812 1.1478 1.9274 +#> 1.0000 2.1000 ||--|| 0.7522 0.7536 0.5742 . . . 0.8812 1.1478 1.9274 +#> 0.0000 5.0000 ||--|| 0.7242 0.7323 0.5690 -0.0562 0.0166 -0.0894 0.8495 1.1776 1.9493 +#> 0.3333 5.0000 ||--|| 0.7522 0.7535 0.5740 . . . 0.8811 1.1479 1.9275 +#> 0.6667 5.0000 ||--|| 0.7522 0.7535 0.5742 . . . 0.8811 1.1479 1.9274 +#> 1.0000 5.0000 ||--|| 0.7522 0.7535 0.5742 . . . 0.8811 1.1478 1.9275 +#> +#> +#> ---------- ---------- ---------- ---------- +#> y4~~y4 y5~~y5 y6~~y6 y7~~y7 +#> ========== ========== ========== ========== +#> 1.0829 0.5684 0.9626 1.5255 +#> 1.0804 0.5710 0.9628 1.5320 +#> 1.0804 0.5710 0.9628 1.5320 +#> 1.0804 0.5710 0.9628 1.5320 +#> 1.0829 0.5684 0.9626 1.5255 +#> 1.0805 0.5711 0.9628 1.5320 +#> 1.0804 0.5710 0.9628 1.5320 +#> 1.0804 0.5710 0.9628 1.5320 ``` ### Selecting a model @@ -278,10 +278,8 @@ head(AICs) #> 5 0.00000000 1 1070.810 1070.810 1070.810 1070.810 13 TRUE 1096.810 fitLasso@parameters[which.min(AICs$AIC),] -#> lambda alpha l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 -#> 1 0.1034089 1 0.7522904 0.753614 0.5742228 0 0 0 0.8812416 1.147737 1.92735 1.080353 0.5710041 0.9628056 -#> y7~~y7 -#> 1 1.531997 +#> lambda alpha l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 +#> 1 0.1034089 1 0.7522904 0.753614 0.5742228 0 0 0 0.8812416 1.147737 1.92735 1.080353 0.5710041 0.9628056 1.531997 ``` An easier way is to use the `coef()` function again: @@ -289,18 +287,18 @@ An easier way is to use the `coef()` function again: ```r coef(fitLasso, criterion = "AIC") -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> lambda alpha ||--|| l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== -#> 0.1034 1.0000 ||--|| 0.7523 0.7536 0.5742 . . . 0.8812 1.1477 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- -#> y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> ========== ========== ========== ========== ========== -#> 1.9273 1.0804 0.5710 0.9628 1.5320 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> lambda alpha ||--|| l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 y3~~y3 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 0.1034 1.0000 ||--|| 0.7523 0.7536 0.5742 . . . 0.8812 1.1477 1.9273 +#> +#> +#> ---------- ---------- ---------- ---------- +#> y4~~y4 y5~~y5 y6~~y6 y7~~y7 +#> ========== ========== ========== ========== +#> 1.0804 0.5710 0.9628 1.5320 ``` Alternatively, you can extract just the estimates with: @@ -343,18 +341,18 @@ The best model can now be accessed with ```r coef(fitCvLsp) -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> lambda theta ||--|| l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== -#> 1.0000 0.1000 ||--|| 0.7523 0.7536 0.5742 . . . 0.8813 1.1477 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- -#> y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 -#> ========== ========== ========== ========== ========== -#> 1.9273 1.0804 0.5710 0.9628 1.5320 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> lambda theta ||--|| l2 l3 l4 l5 l6 l7 y1~~y1 y2~~y2 y3~~y3 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 1.0000 0.1000 ||--|| 0.7523 0.7536 0.5742 . . . 0.8813 1.1477 1.9273 +#> +#> +#> ---------- ---------- ---------- ---------- +#> y4~~y4 y5~~y5 y6~~y6 y7~~y7 +#> ========== ========== ========== ========== +#> 1.0804 0.5710 0.9628 1.5320 ``` ### Missing Data @@ -594,30 +592,30 @@ using information criteria: ```r coef(lassoFit, criterion = "BIC") -#> -#> Tuning ||--|| Estimates -#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ----------- ----------- -#> lambda alpha ||--|| ind60=~x2 ind60=~x3 a1 b1 c1 dem60~ind60 dem65~ind60 -#> ======= ======= ||--|| ========== ========== ========== ========== ========== =========== =========== -#> 0.2216 1.0000 ||--|| 2.1825 1.8189 1.2110 1.1679 1.2340 1.4534 0.5935 -#> -#> -#> ----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -#> dem65~dem60 y1~~y5 y2~~y4 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 -#> =========== ========== ========== ========== ========== ========== ========== ========== ========== ========== -#> 0.8659 0.5552 1.5947 0.7807 0.6537 1.5350 0.0820 0.1177 0.4675 1.7929 -#> -#> -#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------------ ------------ -#> y2~~y2 y3~~y3 y4~~y4 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 -#> ========== ========== ========== ========== ========== ========== ========== ============ ============ -#> 7.3843 5.0175 3.4074 2.2857 4.8977 3.5510 3.4511 0.4480 3.9408 -#> -#> ||--|| Transform -#> ------------ ---------- ---------- ---------- ||--|| ---------- ---------- ---------- -#> dem65~~dem65 delta_a2 delta_b2 delta_c2 ||--|| a2 b2 c2 -#> ============ ========== ========== ========== ||--|| ========== ========== ========== -#> 0.2034 . . . ||--|| 1.2110 1.1679 1.2340 +#> +#> Tuning ||--|| Estimates +#> ------- ------- ||--|| ---------- ---------- ---------- ---------- ---------- ----------- ----------- ----------- ---------- +#> lambda alpha ||--|| ind60=~x2 ind60=~x3 a1 b1 c1 dem60~ind60 dem65~ind60 dem65~dem60 y1~~y5 +#> ======= ======= ||--|| ========== ========== ========== ========== ========== =========== =========== =========== ========== +#> 0.2216 1.0000 ||--|| 2.1825 1.8189 1.2110 1.1679 1.2340 1.4534 0.5935 0.8659 0.5552 +#> +#> +#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +#> y2~~y4 y3~~y7 y4~~y8 y6~~y8 x1~~x1 x2~~x2 x3~~x3 y1~~y1 y2~~y2 y3~~y3 y4~~y4 +#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== +#> 1.5947 0.7807 0.6537 1.5350 0.0820 0.1177 0.4675 1.7929 7.3843 5.0175 3.4074 +#> +#> ||--|| +#> ---------- ---------- ---------- ---------- ------------ ------------ ------------ ---------- ---------- ---------- ||--|| +#> y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65 delta_a2 delta_b2 delta_c2 ||--|| +#> ========== ========== ========== ========== ============ ============ ============ ========== ========== ========== ||--|| +#> 2.2857 4.8977 3.5510 3.4511 0.4480 3.9408 0.2034 . . . ||--|| +#> +#> Transform +#> ---------- ---------- ---------- +#> a2 b2 c2 +#> ========== ========== ========== +#> 1.2110 1.1679 1.2340 ``` More details are provided in `vignette("Parameter-transformations", package = "lessSEM")`.