Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的Bootstrap多项式回归_R_Logistic Regression_Statistics Bootstrap - Fatal编程技术网

R中的Bootstrap多项式回归

R中的Bootstrap多项式回归,r,logistic-regression,statistics-bootstrap,R,Logistic Regression,Statistics Bootstrap,我试图在R中引导一个简单的多项式回归,我得到一个错误: is.data.frame(数据)中出错:找不到对象“d” 真正奇怪的是,我使用的代码(针对这个特定问题进行了调整)与boot包教程中的代码相同,并且当我使用不同的函数(如lm())时,同样的代码也起作用。当然,我在做一些愚蠢的事情,但我看不出是什么。如果有人能帮忙,我会非常感激 这是一个例子: require(foreign) require(nnet) require(boot) # an example for multinomia

我试图在R中引导一个简单的多项式回归,我得到一个错误:

is.data.frame(数据)中出错:找不到对象“d”

真正奇怪的是,我使用的代码(针对这个特定问题进行了调整)与boot包教程中的代码相同,并且当我使用不同的函数(如lm())时,同样的代码也起作用。当然,我在做一些愚蠢的事情,但我看不出是什么。如果有人能帮忙,我会非常感激

这是一个例子:

require(foreign)
require(nnet)
require(boot)

# an example for multinomial logistic regression
ml = read.dta('http://www.ats.ucla.edu/stat/data/hsbdemo.dta')
ml = ml[,c(5,7,3)]

bs <- function(formula, data, indices) {
    d = data[indices,] # allows boot to select sample
    fit = multinom(formula, data=d)
    s = summary(fit)
    return(list(fit$coefficients, fit$standard.errors))
}

# 5 replications
results = list()
results <- boot(
    data=ml, statistic=bs, R=5, parallel='multicore',
    formula=prog~write
)
require(国外)
要求(nnet)
需要(启动)
#多项式logistic回归的一个例子
ml=read.dta('http://www.ats.ucla.edu/stat/data/hsbdemo.dta')
ml=ml[,c(5,7,3)]

bs错误发生在
summary()
部分,而且
multinom()
返回的对象没有
系数
标准。错误
。似乎,
summary.multinom()
反过来会根据数据
d
计算出hessian,但由于某种原因(可能是范围问题)找不到该数据。快速修复方法是添加
Hess=TRUE

bs <- function(formula, data, indices) {
  d = data[indices,] # allows boot to select sample
  fit = multinom(formula, data=d, Hess = TRUE)
  s = summary(fit)
  return( cbind(s$coefficients, s$standard.errors) )
}

# 5 replications
results = list()
results <- boot(
  data=ml, statistic=bs, R=5, parallel='multicore',
  formula=prog~write
)

bs多项式逻辑回归使用
coef()
函数返回系数矩阵。这与返回系数向量的
lm
glm
模型不同

library(foreign)     # read.dta()
library(nnet)        # multinom()
require(boot)        # boot()

# an example for multinomial logistic regression
ml = read.dta('http://www.ats.ucla.edu/stat/data/hsbdemo.dta')
ml = ml[,c(5,7,3)]

names(ml)

bs <- function(formula, data, indices) {
  d = data[indices,] # allows boot to select sample
  fit = multinom(formula, data=d, maxit=1000, trace=FALSE)
  #s = summary(fit)
  #return(list(fit$coefficients, fit$standard.errors))

  estimates <- coef(fit)
  return(t(estimates))
}

# enable parallel

library(parallel)
cl <- makeCluster(2)
clusterExport(cl, "multinom")

# 10000 replications
set.seed(1984)

results <- boot(
  data=ml, statistic=bs, R=10000, parallel = "snow", ncpus=2, cl=cl,
  formula=prog~write
)

# label the estimates

subModelNames <- colnames(results$t0)
varNames <- rownames(results$t0)

results$t0

estNames <- apply(expand.grid(varNames,subModelNames),1,function(x) paste(x,collapse="_"))

estNames

colnames(results$t) <- estNames

# summary of results

library(car)

summary(results)

confint(results, level=0.95, type="norm")
confint(results, level=0.95, type="perc")
confint(results, level=0.95, type="bca")

# plot the results

hist(results, legend="separate")
library(外文)#read.dta()
图书馆(nnet)#多名称()
需要(启动)#启动()
#多项式logistic回归的一个例子
ml=read.dta('http://www.ats.ucla.edu/stat/data/hsbdemo.dta')
ml=ml[,c(5,7,3)]
名称(毫升)

bs请在您的问题中发布
traceback()
的输出。我做了一些调查,这可能会导致一个解决方案:环境中存在一些问题(通过打印str(fit)可以看到)从
bs
-函数内部。当您传递公式时,它被设置为全局,并且公式具有创建它们的环境(请参见?公式)。但现在不知道如何修复它。