使用ns样条拟合进行预测(glmer与ns样条拟合)

使用ns样条拟合进行预测(glmer与ns样条拟合),r,predict,spline,mixed-models,R,Predict,Spline,Mixed Models,我正在使用glmer()函数来确定入侵蚯蚓物种的重量是否对明尼苏达州的4种不同树种有显著影响。我试图在调整其他变量后,预测在一定蚯蚓重量下发生事件的概率(因为这是逻辑回归/树种是否会存活)。我们还考虑使用优势比来比较不同树种之间是否存在差异。所以我想使用predict()函数来指定某个权重值并预测事件的概率 这是摘要(数据)。我们特别关注的是meanwormwt(平均蠕虫重量)。我最终想要的是预测给定一定平均蠕虫重量值的事件概率。例如,“当平均蠕虫重量为0.3246时,发生事件的概率是多少?”

我正在使用
glmer()
函数来确定入侵蚯蚓物种的重量是否对明尼苏达州的4种不同树种有显著影响。我试图在调整其他变量后,预测在一定蚯蚓重量下发生事件的概率(因为这是逻辑回归/树种是否会存活)。我们还考虑使用优势比来比较不同树种之间是否存在差异。所以我想使用
predict()
函数来指定某个权重值并预测事件的概率

这是
摘要(数据)
。我们特别关注的是
meanwormwt
(平均蠕虫重量)。我最终想要的是预测给定一定平均蠕虫重量值的事件概率。例如,“当平均蠕虫重量为0.3246时,发生事件的概率是多少?”

我在平均蚯蚓重量上使用了ns样条线,并将其分为3条样条线。我需要使用什么代码来进行此操作?我尝试过使用predict.ns或predict.merMod,但我不知道如何使用,因为我们不只是寻找总体平均预测值。。我们想看看在一定权重下的预测值。我应该尝试什么命令?我该怎么办

这是我的glmer代码:

```{r}
nsglm<-glmer(Mort16JuneAPF ~ Exclose*Species + ns(meanwormwt, df=3, knots=c(0.3246,1.0901))*Species + (1 | Park) + (1 | Cluster:Park) + (1 | Plot:Cluster:Park) + (1|Exclose:ParkPlot) + (1 | x:Unique), data = mydata, family = binomial, control=glmerControl(optimizer="bobyqa", calc.derivs = FALSE))
summary(nsglm)
```
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
 Family: binomial  ( logit )
Formula: Mort16JuneAPF ~ Exclose * Species + ns(meanwormwt, df = 3, knots = c(0.3246,  
    1.0901)) * Species + (1 | Park) + (1 | Cluster:Park) + (1 |  
    Plot:Cluster:Park) + (1 | Exclose:ParkPlot) + (1 | x:Unique)
   Data: mydata
Control: glmerControl(optimizer = "bobyqa", calc.derivs = FALSE)

     AIC      BIC   logLik deviance df.resid 
  4253.3   4410.2  -2101.7   4203.3     3910 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-4.2758 -0.6492  0.2821  0.6346  4.0010 

Random effects:
 Groups            Name        Variance Std.Dev.
 x:Unique          (Intercept) 0.01345  0.1160  
 Exclose:ParkPlot  (Intercept) 0.51799  0.7197  
 Plot:Cluster:Park (Intercept) 0.00000  0.0000  
 Cluster:Park      (Intercept) 0.28753  0.5362  
 Park              (Intercept) 0.03863  0.1965  
Number of obs: 3935, groups:  
x:Unique, 564; Exclose:ParkPlot, 142; Plot:Cluster:Park, 71; Cluster:Park, 24; Park, 6

Fixed effects:
                                                               Estimate Std. Error z value Pr(>|z|)    
(Intercept)                                                      0.6957     0.4435   1.569 0.116746    
ExcloseYes                                                      -2.7133     0.2090 -12.981  < 2e-16 ***
SpeciesQUMA                                                      1.2551     0.3827   3.279 0.001041 ** 
SpeciesRHCA                                                     -0.6303     0.3407  -1.850 0.064331 .  
SpeciesTIAM                                                     -0.5476     0.3500  -1.565 0.117687    
ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))1               1.2171     0.6496   1.874 0.060986 .  
ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))2               0.8967     0.9645   0.930 0.352534    
ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))3              -0.2013     0.7047  -0.286 0.775132    
ExcloseYes:SpeciesQUMA                                           1.5177     0.2375   6.391 1.65e-10 ***
ExcloseYes:SpeciesRHCA                                           2.2524     0.2138  10.533  < 2e-16 ***
ExcloseYes:SpeciesTIAM                                           1.0164     0.2295   4.430 9.44e-06 ***
SpeciesQUMA:ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))1  -0.3065     0.6130  -0.500 0.617043    
SpeciesRHCA:ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))1  -1.0661     0.5614  -1.899 0.057555 .  
SpeciesTIAM:ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))1   0.6600     0.6074   1.087 0.277240    
SpeciesQUMA:ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))2  -2.1818     0.8225  -2.653 0.007984 ** 
SpeciesRHCA:ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))2  -1.3299     0.7390  -1.800 0.071897 .  
SpeciesTIAM:ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))2   3.0146     0.7774   3.878 0.000105 ***
SpeciesQUMA:ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))3  -2.8120     0.5579  -5.041 4.64e-07 ***
SpeciesRHCA:ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))3  -0.4749     0.5100  -0.931 0.351807    
SpeciesTIAM:ns(meanwormwt, df = 3, knots = c(0.3246, 1.0901))3   2.4477     0.5762   4.248 2.16e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
`{r}
nsglm | z |)
(截距)0.6957 0.4435 1.569 0.116746
不包括-2.7133 0.2090-12.981<2e-16***
规格QUMA 1.2551 0.3827 3.279 0.001041**
规格RHCA-0.6303 0.3407-1.850 0.064331。
规格-0.5476 0.3500-1.565 0.117687
南北向(平均总重量,df=3,节数=c(0.3246,1.0901))11.2171 0.6496 1.874 0.060986。
南北向(平均总重量,df=3,节数=c(0.3246,1.0901))20.8967 0.9645 0.930 0.352534
南北向(平均总重量,df=3,节数=c(0.3246,1.0901))3-0.2013 0.7047-0.286 0.775132
不包括:规格QUMA 1.5177 0.2375 6.391 1.65e-10***
不包括:规格RHCA 2.2524 0.2138 10.533<2e-16***
不包括:规格1.0164 0.2295 4.430 9.44e-06***
种类quma:ns(平均总重量,df=3,节数=c(0.3246,1.0901))1-0.30650.6130-0.500 0.617043
物种RHCA:ns(平均总重量,df=3,节数=c(0.3246,1.0901))1-1.0661 0.5614-1.899 0.057555。
种类:南北(平均总重量,df=3,节数=c(0.3246,1.0901))10.6600.6074 1.087 0.277240
种类quma:ns(平均总重量,df=3,节数=c(0.3246,1.0901))2-2.1818 0.8225-2.653 0.007984**
物种Rhca:ns(平均总重量,df=3,节数=c(0.3246,1.0901))2-1.3299 0.7390-1.800 0.071897。
规格:ns(平均毛重,df=3,节数=c(0.3246,1.0901))23.0146 0.7774 3.878 0.000105***
种类quma:ns(平均总重量,df=3,节数=c(0.3246,1.0901))3-2.8120 0.5579-5.041 4.64e-07***
物种Rhca:ns(平均总重量,df=3,节数=c(0.3246,1.0901))3-0.4749 0.5100-0.931 0.351807
规格:ns(平均总重量,df=3,节数=c(0.3246,1.0901))3 2.4477 0.5762 4.248 2.16e-05***
---
签名。代码:0'***'0.001'***'0.01'*'0.05'.'0.1''1

您需要将
predict()
newdata
参数一起使用。您需要为每个固定效果输入变量指定一些值,例如

nd <- with(mydata,
   expand.grid(Exclose=levels(Exclose), Species=levels(Species))
nd$meanwormwt <- 0.361
predict(nsglm, re.form=~0, newdata=nd)

nd我不明白这个问题。你指的是什么重量?模型重量?你不能“以一定的权重预测”。我不认为权重对模型中的任何地方都有影响。在没有数据的情况下(在承认用户困惑的情况下)提出面向方法的问题时,最好的地方是R混合模型邮件列表。我添加了我的数据。所以有一个叫做meanwormwt的变量,它表示蚯蚓的平均重量。我想预测的是事件发生的概率(例如,给定蚯蚓重量的特定值,蚯蚓重量是否会影响树种存活率),那么,给定蚯蚓重量为0.03,树种存活的概率是多少?这有意义吗?
nd <- with(mydata,
   expand.grid(Exclose=levels(Exclose), Species=levels(Species))
nd$meanwormwt <- 0.361
predict(nsglm, re.form=~0, newdata=nd)