使用jsonlite序列化mgcv gam模型时出错

问题描述 投票:0回答:1

我想以健壮的方式“序列化”具有jsonlite的mgcv gam模型,以便以后与rpy2一起使用。

((我的意思是鲁棒的:当使用python时,我认为Pickle并不鲁棒,因为在每次Python更新之后,序列化管道都会中断。这就是为什么我更喜欢将json格式与jsonlite一起使用,在我看来,它更鲁棒(但我可能错了。)

jsonlite与RJSONIO相比做得很好:除了名为Environnement的子变量之外,mgcv模型的每个细节都已正确序列化:

inspection of the initial model b

inspection of the deserialized model new_b

您可以在前面的两个图像中看到:环境:名称空间:状态变为环境:R_GlobalEnv

我不确定该如何处理。欢迎每一个建议。

这是最小的可复制示例:


> library(mgcv)
> n = 40
> x <- 1:n/n  # data between [0, 1]
> x2 <- 1:n/n  # data between [0, 1]
> x3 <- 1:n/n  # data between [0, 1]
> mu <- exp(-400*(x-.6)^2)+5*exp(-500*(x-.75)^2)/3+2*exp(-500*(x-.9)^2)
> y <- mu+0.5*rnorm(n)
> b <- gam(y~s(x)+te(x2, x3))
> 
> library(jsonlite)
> json_str = serializeJSON(b)
> new_b = unserializeJSON(json_str)
> mat     <- predict.gam(b    , type = "terms")
> new_mat <- predict.gam(new_b, type = "terms")
Error in if (object$by != "NA") { : 
  valeur manquante là où TRUE / FALSE est requis
> 
r rpy2 gam jsonlite mgcv
1个回答
0
投票

[jsonlite中存在问题:“ AN”被序列化为AN

快速修复:

new_b$smooth[[2]]$by = "AN"
new_b$smooth[[1]]$by = "AN"
new_mat <- predict.gam(new_b, type = "terms")


© www.soinside.com 2019 - 2024. All rights reserved.