jslefche / piecewiseSEM

Piecewise Structural Equation Modeling in R
157 stars 49 forks source link

piecewiseSEM with GAMs - Error in `[<-.data.frame`(`*tmp*`, which(ret$Predictor %in% c("(Intercept)", : replacement has 7 rows, data has 2 #293

Open RJCairncross opened 1 year ago

RJCairncross commented 1 year ago

Hi Jon, hope you are well.

I am experiencing the following error when modelling a psem with six component GAMs within piecewiseSEM version 2.3.0, upon running the summary() function after compiling the relevant models:

Error in [<-.data.frame(*tmp*, which(ret$Predictor %in% c("(Intercept)", : replacement has 7 rows, data has 2

I have seen a similar issue from an earlier version of psem raised in a now closed comment, but since this issue has reappeared in the latest version, and the previous issues appeared to be related to an older version of the package, I wanted to ask again.

I am happy to provide relevant code and data if you need it.

Many thanks,

andisa01 commented 4 months ago

Hello, getting the same error when using gam models with summary. Here is a reprex:

set.seed(666)

n = 2000

frog_dat <- tibble( canopy = runif(n = n, min = 0, max = 1), e_1 = rnorm(n = n), e_2 = rnorm(n = n), e_3 = rnorm(n = n), maternal_effect = sample(rnorm(n = 10, sd = 2), size = n, replace = TRUE) ) %>% mutate( pond_temp = canopy + e_1, embryo_vol = canopy + pond_temp + maternal_effect + e_2, dev_rate = ifelse( pond_temp < median(pond_temp), pond_temp + embryo_vol + e_3, -2*pond_temp + embryo_vol + e_3 ) )

psem_02 <- psem( lm(pond_temp ~ canopy, data = frog_dat), lmer(embryo_vol ~ canopy + pond_temp + (1|maternal_effect), data = frog_dat), gam(dev_rate ~ embryo_vol + s(pond_temp, bs = 'cs'), family = gaussian, data = frog_dat), data = frog_dat )

summary(psem_02)

Yields this error:

Error in [<-.data.frame(*tmp*, which(ret$Predictor %in% c("(Intercept)", : replacement has 2 rows, data has 1

dumaskvn commented 3 months ago

I was investigating this issue recently, and noticed that it happens only when mixing smoothed terms and linear terms in a gam formula (cf MRE). Here I use the coefs function to illustrate the bug as its output is shorter than summary but the same bug occurs of course when using summary.

Many thanks for investigating this issue !

MRE:

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(nlme)
library(lme4)
#> Loading required package: Matrix
#> 
#> Attaching package: 'lme4'
#> The following object is masked from 'package:nlme':
#> 
#>     lmList
library(mgcv)
#> 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 <LefcheckJ@si.edu>.

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

coefs(psem1)
#> Warning: Categorical or non-linear variables detected. Please refer to
#> documentation for interpretation of Estimates!
#>   Response Predictor Estimate Std.Error       DF Crit.Value P.Value
#> 1       x2     s(x1)        -         -   3.2242    41.2590   0e+00
#> 2       x3        x2   0.1464    0.0373  98.0000     3.9248   1e-04
#> 3       x4        x2   0.1741     0.038 100.0000     4.5856   0e+00
#>   Std.Estimate    
#> 1            - ***
#> 2        0.363 ***
#> 3       0.3565 ***

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

coefs(psem2)
#> Error in `[<-.data.frame`(`*tmp*`, which(ret$Predictor %in% c("(Intercept)", : replacement has 2 rows, data has 1

Created on 2024-08-20 with reprex v2.1.0