R Shiny Tool适用于模拟数据,而不是真实数据(t.default(inputdata)中的错误:参数不是矩阵)

时间:2015-02-23 01:02:18

标签: r shiny

我的r闪亮工具有问题。我努力工作以复制问题,并使用该工具处理模拟数据但我收到错误:

Error in t.default(inputdata) : argument is not a matrix
每当我尝试使用我的真实数据集运行相同的代码时,

来自server.R。

我正在创建的工具是一个用于分段的简单打字工具,我想在选择一些特征时有一个预测类的单个输出。以下是适用于模拟数据的工具代码:

# Simulation of the Analysis
library(MASS)
library(poLCA)

#Simulate data set
V1 <- sample( LETTERS[1:5], 521, replace=TRUE, prob=c(0.1, 0.2, 0.45, 0.05, 0.2) )
V2 <- sample( LETTERS[1:5], 521, replace=TRUE, prob=c(0.3, 0.2, 0.1, 0.25, 0.15) )
V3 <- sample( LETTERS[1:5], 521, replace=TRUE, prob=c(0.7, 0.05, 0.1, 0.05, 0.01) )
V4 <- sample( LETTERS[1:3], 521, replace=TRUE, prob=c(0.1, 0.25, 0.65) )
V5 <- sample( LETTERS[1:3], 521, replace=TRUE, prob=c(0.65, 0.2, 0.15) )
V6 <- sample( LETTERS[1:3], 521, replace=TRUE, prob=c(0.5, 0.45, 0.05) )
V7 <- sample( LETTERS[1:2], 521, replace=TRUE, prob=c(0.3, 0.7) )
V8 <- sample( LETTERS[1:2], 521, replace=TRUE, prob=c(0.55, 0.45) )
V9 <- sample( LETTERS[1:2], 521, replace=TRUE, prob=c(0.8, 0.2) )
V10 <- sample( LETTERS[1:2], 521, replace=TRUE, prob=c(0.6, 0.4) )
test.data <- cbind(V1, V2, V3, V4, V5, V6, V7, V8, V9, V10)
test.data <- as.data.frame(test.data)

#Simulate LCA 
test.lca.formula <- cbind(V1, V2, V3, V4, V5, V6, V7, V8, V9, V10) ~ 1
test.lca <- poLCA(test.lca.formula, test.data, nclass = 5, maxiter = 10000)
testlca.predclass <- test.lca$predclass
test.data <- cbind(test.data, testlca.predclass)

#Simulate LDA
attach(test.data)
test.lda <- lda(testlca.predclass ~ V1 + V4 + V7, test.data)
testlda.predict <- predict(test.lda)
testldapredict.class <- (testlda.predict$class)
testclasstab.realvpred <- table(testldapredict.class, testlca.predclass)
testclasstab.realvpred

#Simulate Trained LDA
testldamodel.traindata <- sort(sample(1:521, 100))
test.lda2 <- lda(testlca.predclass ~ V1 + V4 + V7, test.data, subset=testldamodel.traindata)
testlda2.predict <- predict(test.lda2)
testlda2predict.class <- (testlda2.predict$class)
testclasstab.realvpred2 <- table(testlda2predict.class, (testlca.predclass[testldamodel.traindata]))
testclasstab.realvpred2

#server.R
library(shiny)
test.lda2 <- lda(testlca.predclass ~ V1 + V4 + V7, test.data, subset=testldamodel.traindata)
class <- function(v1, v4, v7){
inputdata <- c(v1, v4, v7)
pred_data <- as.data.frame(t(inputdata))
colnames(pred_data) <- c("V1", "V4", "V7")
testlda2.predictions <- predict(test.lda2, pred_data)
testlda2predictions.class <- testlda2.predictions$class
return(testlda2predictions.class)
}
shinyServer(
function(input, output) {
output$class <- renderText({class(input$v1, input$v4, input$v7)
})    
})

#ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("User Class Prediction Tool"),  
sidebarPanel(
selectInput("v1", label = h4("Mustach:"),
            list ("Soul Patch" = "A", "Regular" = "B", "Handle Bar" = "C", "Walrus" = "D", "ZZ Top" = "E")),    
selectInput("v4", label = h4("Hair:"),
            list("None" = "A", "Some" = "B", "Lots" = "C")),
selectInput("v7", label = h4("Sun Glasses:"),
            list ("Yes" = "A", "No" = "B"))
),  
mainPanel(
tabPanel("Class", h2(textOutput('class'))
))
)
)

根据我收到的错误,(inputdata)变量无法转置,因为它不是矩阵。但它在模拟工具中运行得很好。这是我的真实数据集的server.R代码:

library(shiny)

ldamodel2 <- lda(predclass ~ Ageord2 + Hsehld_Incomeord6catchar2 + HlthStatusord2 + Hlth_Coverage_Typeord2 + WellnessEffort_HadVisitord2 + Genderord2 + Engagement_AlternativeMedord2 + Enguagement_RecievedERcareord2 + Engagement_TraveledIntlforCareord2, mixmodel, subset=ldamodel.traindata2)


pred_class <- function(ageord2, hsehld_incomeord6catchar2, hlthstatusord2, hlth_coverage_typeord2, wellnesseffort_hadvisitord2, genderord2, engagement_alternativemedord2, enguagement_recievedercareord2, engagement_traveledintlforcareord2){
inputdata <- c(ageord2, hsehld_incomeord6catchar2, hlthstatusord2, hlth_coverage_typeord2, wellnesseffort_hadvisitord2, genderord2, engagement_alternativemedord2, enguagement_recievedercareord2, engagement_traveledintlforcareord2)
pred_data <- as.data.frame(t(inputdata))
colnames(pred_data) <- c("Ageord2", "Hsehld_Incomeord6catchar2", "HlthStatusord2", "Hlth_Coverage_Typeord2", "WellnessEffort_HadVisitord2", "Genderord2", "Engagement_AlternativeMedord2", "Enguagement_RecievedERcareord2", "Engagement_TraveledIntlforCareord2")
ldamodel.predictions2 <- predict(ldamodel2, pred_data)
ldamodel.predictions2class <- ldamodel.predictions2$class
return(ldamodel.predictions2class)
}

shinyServer(
function(input, output) {
output$class <- renderText({pred_class(input$ageord2, input$hsehld_incomeord6catchar2, input$hlthstatusord2, input$hlth_coverage_typeord2, input$wellnesseffort_hadvisitord2, input$genderord2, input$engagement_alternativemedord2, input$enguagement_recievedercareord2, input$engagement_traveledintlforcareord2)
})

})

我意识到我无法重新创建错误,因为模拟有效!但是如果有人在这种情况下遇到过这个错误,我对r闪亮很新,这将是一个很大的帮助。它也可能是我看不到的代码中令人难以置信的愚蠢,因为它太长了。即使从两个人那里猜测也会有所帮助。谢谢!

0 个答案:

没有答案