Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LLChisq in GAMs differ from example #311

Open
dumaskvn opened this issue Aug 20, 2024 · 0 comments
Open

LLChisq in GAMs differ from example #311

dumaskvn opened this issue Aug 20, 2024 · 0 comments

Comments

@dumaskvn
Copy link

Hi !
First many thanks for maintaining this amazing package.

I recently got interested in using gam models within SEMs built with piecewise (althought this is not going well due to #293 ).

While going through your excellent book (https://jslefche.github.io/sem_book/local-estimation.html#extensions-to-non-linear-models), I noticed that log-likelihood Chi2 values differ between the book and the actual results.

set.seed(100)
n <- 100
x1 <- rchisq(n, 7)
mu2 <- 10*x1/(5 + x1)
x2 <- rnorm(n, mu2, 1)
x2[x2 <= 0] <- 0.1
x3 <- rpois(n, lambda = (0.5*x2))
x4 <- rpois(n, lambda = (0.5*x2))
p.x5 <- exp(-0.5*x3 + 0.5*x4)/(1 + exp(-0.5*x3 + 0.5*x4))
x5 <- rbinom(n, size = 1, prob = p.x5)
dat2 <- data.frame(x1 = x1, x2 = x2, x3 = x3, x4 = x4, x5 = x5)

library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.9-1. For overview type 'help("mgcv-package")'.
library(piecewiseSEM)
#> 
#>   This is piecewiseSEM version 2.3.0.
#> 
#> 
#>   Questions or bugs can be addressed to <[email protected]>.

shipley_psem2 <- psem(
  lm(x2 ~ x1, data = dat2),
  lm(x3 ~ x2, data = dat2),
  lm(x4 ~ x2, data = dat2),
  lm(x5 ~ x3 + x4, data = dat2)
)

LLchisq(shipley_psem2)
#>   Chisq df P.Value
#> 1 4.143  5   0.529

The above p-value and Chisq stat are consistent with the one found in your book, while

shipley_psem3 <- psem(
  gam(x2 ~ s(x1), data = dat2, family = gaussian),
  glm(x3 ~ x2, data = dat2, family = poisson),
  gam(x4 ~ x2, data = dat2, family = poisson),
  glm(x5 ~ x3 + x4, data = dat2, family = binomial)
)

LLchisq(shipley_psem3)
#>    Chisq     df P.Value
#> 1 19.737 10.291   0.036

These differs, the expected values from your example being :

##   Chisq df P.Value
## 1 3.346  5   0.647

I thus would like your confirmation that the results obtained when using GAMs are still consistent.
Many thanks for your support !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant