在R中对MANCOVA应用boosterapping(引导和MANCOVA功能)

时间:2018-08-27 21:24:18

标签: r statistics-bootstrap

我一直在尝试在启动功能中实现MANCOVA,并继续遇到以下错误。任何帮助将不胜感激!

library(boot)
library (jmv)
DATA <- read.csv("DATA.csv", header = T)
STAT <- function(DATA) {
    mancova(data = DATA,
        deps = cbind('FAC1', 'FAC2', 'FAC3', 'FAC4', 'FAC5'),
        factors = 'subtype',
        covs = 'age',
        multivar = 'pillai',
        boxM = FALSE,
        shapiro = FALSE,
        qqPlot = FALSE)
}
BS.OUT = boot(DATA, statistic = STAT, R = 1000, sim = 'parametric')
  

t.star [r,] <-res [[r]]中的错误:    要替换的项目数不是替换长度的倍数

不太确定如何解决此问题。任何建议将不胜感激!

谢谢, A

1 个答案:

答案 0 :(得分:0)

您遇到的问题是因为您获取的形状数据不同。您可以在函数中指定数据的形状。查看是否these链接help

我提供了一些随机数据作为示例。

Var1 = runif(10, 3, 30)
Var2 = runif(10, 2, 5)
Y <- cbind(Var1, Var2)
Age <- runif(10, 20, 30)
Sex <- gl(2, 5, labels = c("male", "female"))
df <- as.data.frame(cbind(Y, Age, Sex))
df$Sex <- as.factor(df$Sex)

stat_test_func <- function(x, indices) {
  x$Var1 <- x$Var1[indices]
  model <- glm(Var1 + Var2 ~ Sex*Age, data = x)
  return(coefs = coef(model))
}

bs.out <- boot(df, statistic = stat_test_func, R = 10, sim = "parametric")

> bs.out

PARAMETRIC BOOTSTRAP

Call:
boot(data = df, statistic = stat_test_func, R = 10, sim = "parametric")

Bootstrap Statistics :
       original  bias    std. error
t1*  131.650088       0           0
t2* -132.423883       0           0
t3*   -4.659926       0           0
t4*    5.804188       0           0

编辑

在进一步阅读?boot之后,看来对于参数引导程序,您需要提供随机生成函数ran.gen。但是我在遇到同样的错误时遇到了麻烦。可能每个引导程序都不相等,即因素不相等,但我不确定。删除年龄并没有改变它。

stat_test_func <- function(x) {
  mancova(x, deps = c("Var1", "Var2"), factors = "Sex", multivar = "pillai")  
    }

stat_test_rg <- function(x, mle) {
  out <- x
  out$Var1 <- rexp(nrow(out), 1/mle)
  out$Var2 <- rexp(nrow(out), 1/mle)
  out
  print(out)
}

bs.out <- boot(df, statistic = stat_test_func, R = 10, sim = "parametric", ran.gen = stat_test_rg, mle = mean(df$Var1))

Error in t.star[r, ] <- res[[r]] : 
  number of items to replace is not a multiple of replacement length

如果您将年龄作为因素,我会得到different error。因此,此答案可能不再有用。

Var1 = runif(10, 3, 30)
Var2 = runif(10, 2, 5)
Age <- gl(2, 5, labels = c("young", "old"))
# Age <- runif(10, 20, 30)
Sex <- gl(2, 5, labels = c("male", "female"))
Y <- cbind(Var1, Var2)
df <- as.data.frame(cbind(Y, Age, Sex))
df$Sex <- as.factor(df$Sex)
df$Age <- as.factor(df$Age)

Error: Table$setRow(): value 'stat[pillai]' is not atomic