使用Nagelkerke R-Squared在R中引导

时间:2016-08-24 12:03:42

标签: r bootstrapping

我是R的新手。我尝试使用R中的boot()函数,使用Nagelkerke R平方作为统计。我知道我需要一个函数来测量重新采样的原始Nagelkerke R平方。但是,我不知道我应该把它作为统计函数。

我知道Nagelkerke R-squared可以通过使用偏差和Null.deviance给出Logit回归来计算。我写函数来计算Nagelkerke R平方。

  NagR2 <- function(Objects){

  n <- nrow(Objects)

  reg <- glm(form,
                    family = binomial("logit"), data = datainput)

  mo <- stepAIC(regression,direction = c("backward"), trace = FALSE)


  R2cox <-  1- exp((mo$deviance - mo$null.deviance)/n)
  R2nag <-  R2cox/(1-exp((-mo$null.deviance)/n))
  R2nag
  }

我应该如何更改我的NagR2功能,以便在Boot()函数中将其用作统计信息?

1 个答案:

答案 0 :(得分:0)

您需要更改函数,以将输入data.frame作为第一个参数,并以data.frame的索引作为第二个参数和其他参数,因此请稍微更改现有函数:

NagR2 <- function(datainput,ind,form){

  n <- nrow(datainput[ind,])
  reg <- glm(form,family = binomial("logit"), data = datainput[ind,])
  mo <- stepAIC(reg,direction = c("backward"), trace = FALSE)
  
  R2cox <-  1- exp((mo$deviance - mo$null.deviance)/n)
  R2nag <-  R2cox/(1-exp((-mo$null.deviance)/n))
  R2nag
  }

并应用于测试数据集:

library(MASS)
library(boot)

dat = iris
dat$Species=factor(ifelse(dat$Species=="versicolor","v","o"))

bo = boot(dat,statistic=NagR2,R=100,form = as.formula(Species ~ .))

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = dat, statistic = NagR2, R = 100, form = as.formula(Species ~ 
    .))


Bootstrap Statistics :
     original     bias    std. error
t1* 0.3650395 0.01470299   0.0720022