ZheyuanLi / SplinesUtils

GNU General Public License v3.0
11 stars 2 forks source link

glm() objects do not have assign attribute #3

Open yjml opened 3 years ago

yjml commented 3 years ago

glm() and derived objects don't appear to have the assign attribute that RegSplineAsPiecePoly looks for to pull the coefficients out.

e.g. changing lm() in the example to glm()

## a toy data set
set.seed(0)
## a curve of 'x' (note that my 'x' are not uniformly sampled)
x <- sort(rnorm(400, 0, pi))
fx <- sin(x) + 0.2 * x + cos(abs(x))
x_range <- range(x)
## a 2-level grouping variable (1st group with mean 0 and 2nd group with mean 1)
g <- sample(gl(2, 200, labels = letters[1:2]))
fg <- c(0, 1)[g]
## a linear effect
u <- runif(400, x_range[1], x_range[2])
fu <- -0.05 * u
## a spurious covariate
z <- runif(400, x_range[1], x_range[2])
## an additive model
y <- fx + fg + fu + rnorm(400, 0, 0.5)

##### glm() here instead of lm() #####
fit <- glm(y ~ g + bs(x, df = 20) + u + ns(z, df = 10))
> spl_bs <- RegSplineAsPiecePoly(fit, "bs(x, df = 20)")
Error in assign == pos : 
  comparison (1) is possible only for atomic and list types
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Amazon Linux 2

Matrix products: default
BLAS:   /opt/R/4.0.3/lib64/R/lib/libRblas.so
LAPACK: /opt/R/4.0.3/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] SplinesUtils_0.2

loaded via a namespace (and not attached):
[1] compiler_4.0.3 tools_4.0.3