2014-06-16 6 views
5

bayesglm을 사용할 때 예측 기능에 문제가 있습니다. 필자는 샘플 데이터가 부족한 경우 샘플 데이터보다 더 많은 레벨이있을 때이 문제가 발생할 수 있다고 말하는 게시물을 읽었지 만 적합 및 예측 기능에 동일한 데이터를 사용하고 있습니다. Predict는 보통 glm으로는 정상적으로 작동하지만 bayesglm에서는 작동하지 않습니다. 예 :베이 스 예측, 경계 첨자

control <- y ~ x1 + x2 

# this works fine: 
glmObject <- glm(control, myData, family = binomial()) 
predicted1 <- predict.glm(glmObject , myData, type = "response") 

# this gives an error: 
bayesglmObject <- bayesglm(control, myData, family = binomial()) 
predicted2 <- predict.bayesglm(bayesglmObject , myData, type = "response") 
Error in X[, piv, drop = FALSE] : subscript out of bounds 

# Edit... I just discovered this works. 
# Should I be concerned about using these results? 
# Not sure why is fails when I specify the dataset 
predicted3 <- predict(bayesglmObject, type = "response") 

bayesglm 개체를 사용하여 예측하는 방법을 알아낼 수 없습니다. 어떤 아이디어? 감사!

답변

2

bayesglm 명령의 매개 변수 "drop.unused.levels"에 대한 기본 설정으로 할 수있는 이유 중 하나가 있습니다. 기본적으로이 매개 변수는 TRUE로 설정됩니다. 따라서 사용되지 않는 레벨이있는 ​​경우 모델 작성 중에 삭제됩니다. 그러나 예측 기능은 요인 변수에 사용되지 않은 수준이있는 원본 데이터를 계속 사용합니다. 이로 인해 모델 작성에 사용 된 데이터와 예측에 사용 된 데이터 간의 레벨 차이가 발생합니다 (myData의 경우에도 동일한 데이터 평가 임). 나는 예를 들어 아래있다 :

n <- 100 
    x1 <- rnorm (n) 
    x2 <- as.factor(sample(c(1,2,3),n,replace = TRUE)) 

    # Replacing 3 with 2 makes the level = 3 as unused 
    x2[x2==3] <- 2 

    y <- as.factor(sample(c(1,2),n,replace = TRUE)) 

    myData <- data.frame(x1 = x1, x2 = x2, y = y) 
    control <- y ~ x1 + x2 

    # this works fine: 
    glmObject <- glm(control, myData, family = binomial()) 
    predicted1 <- predict.glm(glmObject , myData, type = "response") 

    # this gives an error - this uses default drop.unused.levels = TRUE 
    bayesglmObject <- bayesglm(control, myData, family = binomial()) 
    predicted2 <- predict.bayesglm(bayesglmObject , myData, type = "response") 

    Error in X[, piv, drop = FALSE] : subscript out of bounds 

    # this works fine - value of drop.unused.levels is set to FALSE 
    bayesglmObject <- bayesglm(control, myData, family = binomial(),drop.unused.levels = FALSE) 
    predicted2 <- predict.bayesglm(bayesglmObject , myData, type = "response") 

나는 더 좋은 방법은 사전에 데이터 프레임에서 사용되지 않는 수준을 드롭 모델 구축 및 예측에 모두 사용하는 droplevels을 사용하는 것입니다 생각합니다.