如何在 data.frame() 中使用 reactiveElements
How to use reactiveElements inside data.frame()
我已经将我的数据集导入到我的 app.R 中。将用户输入添加到数据帧以及如何在预测后将输出设为是或否以及在使用预测()后显示输出时存在问题
这是我的 ui 仪表板主体:
box(mainPanel("ENTER THE DETAILS :",br(),br(),textInput("name","Name :","Name Here"),br(),
numericInput("uiage",'Age :',value = 25,min = 25,max = 100),br(),
radioButtons("uigender","Gender :",c("Male","Female"),inline=TRUE),br(),
textInput("uipurpose","Purpose Of Loan :","Car Loan"),br(),
numericInput("uicredhist",'Cibil Score :',min=1,max=10,value = 9),br(),
numericInput("uicredamt", 'Credit Amount',value = 10000),br(),
numericInput("uicommitment","Intallment Commitment",value =2,min=0,max=6),br(),
radioButtons("uiemplymentstatus","Employment Status :",c("Yes","No"),inline = TRUE),br(),br(),
numericInput("uicheckingstatus",'Checking Status :',min=1,max=10,value = 9),br(),
textInput("uipropmag","Property Magnitude:","real-estate/life-insurance/car/jewels"),br(),
radioButtons("uihousing","Housing :",c("own","rent"),inline = TRUE),br(),
radioButtons("uiforeign","Foreign Worker :",c("yes","no"),inline=TRUE),br(),
radioButtons("uieducation","Educational Qualities :",c("Skilled","Un-Skilled"),inline = TRUE),br(),
actionButton("submit",label = "Submit"),textOutput("text1")))
This is my server logic to get user inputs :
text_reactive1 <- eventReactive( input$submit, {
userage <- input$uiage
})
text_reactive2 <- eventReactive(input$submit,{
usercredithist <- input$uicredhist
})
text_reactive3 <- eventReactive(input$submit,{
usercredamt <- input$uicredamt
})
text_reactive4 <- eventReactive(input$submit,{
usercommit <- input$uicommitment
})
this is my algorithm :
fit <- randomForest(df$class~df$age+df$credit_history+df$credit_amount+df$installment_commitment, data=datfr$df)
randpred <-reactive({ predict(fit,dframe=data.frame(text_reactive1(),text_reactive2(),text_reactive3(),text_reactive4()))})
output$text1 <- renderText({
paste("Input cred hist is :",randpred())
})
我需要我的输出是或否..有人能帮我解决这个问题吗..
关于这个问题有很多事情正在发生,但不是很清楚。
我假设这些是您需要帮助的以下问题
- 正确应用随机森林
- 正在访问来自
shiny
的输入值
随机森林
你的代码有很多问题是使用了不正确的参数,因为你没有提供 reproducible example 数据,我将继续使用 iris
数据集。
fit <- randomForest(formula = Species~Sepal.Width+Sepal.Length,
data = iris)
当您使用公式表示法调用 randomForest
时,您不需要(而且可能不应该)使用 $
访问向量,而是使用它们的符号名称。这主要是与对新数据使用 predict
的区别。
predict
关于在新数据上测试模型的论点是 newdata
而不是 dframe
。由于 dframe
永远不会被评估(因为惰性评估),predict
函数的默认值只是 return 原始预测值的值。我会在一秒钟内回到你的预测值,但首先我想展示你需要做什么
使用 newdata
参数。
#If I did random forests accessing vectors with $
dim(iris)
#[1] 150 5
fit <- randomForest(formula = iris$Species~iris$Sepal.Width+iris$Sepal.Length,
data = iris)
predict(fit) # default returns predicted values
predict(fit, newdata = data.frame(Sepal.width = c(1:10), Sepal.Length = c(21:30)) #throws an error
#Error in x[...] <- m : NAs are not allowed in subscripted assignments
#In addition: Warning message:
#'newdata' had 10 rows but variables found have 150 rows
因为 randomForest
formula
参数被赋予了一个长度为 150 的向量,它期望新的数据帧是那个大小。这仍然是一个问题,因为即使满足 newdata
尺寸,拟合几乎总是相同的...
test1 <- predict(fit, newdata = data.frame(rnorm(150,2194192,409), rnrom(150, -12359,21885999)))
test2 <- predict(fit, newdata = data.frame(rnorm(150, 10000),rnorm(150, -1000000)))
all(test1==test2)
#TRUE
使用正确的记法,则:
fit <- randomForest(formula = Species~Sepal.Width+Sepal.Length,
data = iris)
predict(fit, newdata = data.frame(Sepal.Width = 1:10, Sepal.Length = 10:1))
# 1 2 3 4 5 6 7 8 9 10
#virginica virginica virginica virginica setosa setosa setosa setosa setosa setosa
#Levels: setosa versicolor virginica
注意:newdata
数据框必须与 randomForest
拟合中的响应值具有相同的列 headers,否则您会报错。
现在我想谈谈你的预测变量。由于您没有提供数据示例,因此我假设 df$class
是您要分类的内容。由于您的 predict
输出为您提供了一个数字 - 我猜 df$class
是一个连续值或存储为 0
或 1
的整数。
如果您希望 randomForest
return 为 "yes"
或 "no"
,则 df$class
应该是包含 "yes"
或 "no"
.
服务器逻辑
同样,我不确定您提供的代码中是否存在任何错误,因为没有可重现的示例,但我会这样压缩您的服务器逻辑。
server <- function(input, output){
#somewhere load and assign datfr
fit <- randomForest(class~age+credit_history+credit_amount+installment_commitment,
data=datfr$df)
rv <- reactiveValues(userage = NULL,
usercredithist = NULL,
usercredamt = NULL,
usercommit = NULL)
observeEvent(input$submit,{
req(input$uiage, input$uicredhist, input$uicredamt, input$uicommitment)
rv$userage <- input$uiage
rv$usercredithist <- input$uicredhist
rv$usercredamt <- input$uicredamt
rv$usercommit <- input$uicommitment
}
)
randpred <-reactive({
predict(fit,
newdata=data.frame(age=rv$userage,
credit_history=rv$usercredithist,
credit_amount=rv$usercredamt,
installment_commitment=rv$usercommit))
})
output$text1 <- renderText({
paste("Input cred hist is :",randpred())
})
我已经将我的数据集导入到我的 app.R 中。将用户输入添加到数据帧以及如何在预测后将输出设为是或否以及在使用预测()后显示输出时存在问题 这是我的 ui 仪表板主体:
box(mainPanel("ENTER THE DETAILS :",br(),br(),textInput("name","Name :","Name Here"),br(),
numericInput("uiage",'Age :',value = 25,min = 25,max = 100),br(),
radioButtons("uigender","Gender :",c("Male","Female"),inline=TRUE),br(),
textInput("uipurpose","Purpose Of Loan :","Car Loan"),br(),
numericInput("uicredhist",'Cibil Score :',min=1,max=10,value = 9),br(),
numericInput("uicredamt", 'Credit Amount',value = 10000),br(),
numericInput("uicommitment","Intallment Commitment",value =2,min=0,max=6),br(),
radioButtons("uiemplymentstatus","Employment Status :",c("Yes","No"),inline = TRUE),br(),br(),
numericInput("uicheckingstatus",'Checking Status :',min=1,max=10,value = 9),br(),
textInput("uipropmag","Property Magnitude:","real-estate/life-insurance/car/jewels"),br(),
radioButtons("uihousing","Housing :",c("own","rent"),inline = TRUE),br(),
radioButtons("uiforeign","Foreign Worker :",c("yes","no"),inline=TRUE),br(),
radioButtons("uieducation","Educational Qualities :",c("Skilled","Un-Skilled"),inline = TRUE),br(),
actionButton("submit",label = "Submit"),textOutput("text1")))
This is my server logic to get user inputs :
text_reactive1 <- eventReactive( input$submit, {
userage <- input$uiage
})
text_reactive2 <- eventReactive(input$submit,{
usercredithist <- input$uicredhist
})
text_reactive3 <- eventReactive(input$submit,{
usercredamt <- input$uicredamt
})
text_reactive4 <- eventReactive(input$submit,{
usercommit <- input$uicommitment
})
this is my algorithm :
fit <- randomForest(df$class~df$age+df$credit_history+df$credit_amount+df$installment_commitment, data=datfr$df)
randpred <-reactive({ predict(fit,dframe=data.frame(text_reactive1(),text_reactive2(),text_reactive3(),text_reactive4()))})
output$text1 <- renderText({
paste("Input cred hist is :",randpred())
})
我需要我的输出是或否..有人能帮我解决这个问题吗..
关于这个问题有很多事情正在发生,但不是很清楚。
我假设这些是您需要帮助的以下问题
- 正确应用随机森林
- 正在访问来自
shiny
的输入值
随机森林
你的代码有很多问题是使用了不正确的参数,因为你没有提供 reproducible example 数据,我将继续使用 iris
数据集。
fit <- randomForest(formula = Species~Sepal.Width+Sepal.Length,
data = iris)
当您使用公式表示法调用 randomForest
时,您不需要(而且可能不应该)使用 $
访问向量,而是使用它们的符号名称。这主要是与对新数据使用 predict
的区别。
predict
关于在新数据上测试模型的论点是 newdata
而不是 dframe
。由于 dframe
永远不会被评估(因为惰性评估),predict
函数的默认值只是 return 原始预测值的值。我会在一秒钟内回到你的预测值,但首先我想展示你需要做什么
使用 newdata
参数。
#If I did random forests accessing vectors with $
dim(iris)
#[1] 150 5
fit <- randomForest(formula = iris$Species~iris$Sepal.Width+iris$Sepal.Length,
data = iris)
predict(fit) # default returns predicted values
predict(fit, newdata = data.frame(Sepal.width = c(1:10), Sepal.Length = c(21:30)) #throws an error
#Error in x[...] <- m : NAs are not allowed in subscripted assignments
#In addition: Warning message:
#'newdata' had 10 rows but variables found have 150 rows
因为 randomForest
formula
参数被赋予了一个长度为 150 的向量,它期望新的数据帧是那个大小。这仍然是一个问题,因为即使满足 newdata
尺寸,拟合几乎总是相同的...
test1 <- predict(fit, newdata = data.frame(rnorm(150,2194192,409), rnrom(150, -12359,21885999)))
test2 <- predict(fit, newdata = data.frame(rnorm(150, 10000),rnorm(150, -1000000)))
all(test1==test2)
#TRUE
使用正确的记法,则:
fit <- randomForest(formula = Species~Sepal.Width+Sepal.Length,
data = iris)
predict(fit, newdata = data.frame(Sepal.Width = 1:10, Sepal.Length = 10:1))
# 1 2 3 4 5 6 7 8 9 10
#virginica virginica virginica virginica setosa setosa setosa setosa setosa setosa
#Levels: setosa versicolor virginica
注意:newdata
数据框必须与 randomForest
拟合中的响应值具有相同的列 headers,否则您会报错。
现在我想谈谈你的预测变量。由于您没有提供数据示例,因此我假设 df$class
是您要分类的内容。由于您的 predict
输出为您提供了一个数字 - 我猜 df$class
是一个连续值或存储为 0
或 1
的整数。
如果您希望 randomForest
return 为 "yes"
或 "no"
,则 df$class
应该是包含 "yes"
或 "no"
.
服务器逻辑
同样,我不确定您提供的代码中是否存在任何错误,因为没有可重现的示例,但我会这样压缩您的服务器逻辑。
server <- function(input, output){
#somewhere load and assign datfr
fit <- randomForest(class~age+credit_history+credit_amount+installment_commitment,
data=datfr$df)
rv <- reactiveValues(userage = NULL,
usercredithist = NULL,
usercredamt = NULL,
usercommit = NULL)
observeEvent(input$submit,{
req(input$uiage, input$uicredhist, input$uicredamt, input$uicommitment)
rv$userage <- input$uiage
rv$usercredithist <- input$uicredhist
rv$usercredamt <- input$uicredamt
rv$usercommit <- input$uicommitment
}
)
randpred <-reactive({
predict(fit,
newdata=data.frame(age=rv$userage,
credit_history=rv$usercredithist,
credit_amount=rv$usercredamt,
installment_commitment=rv$usercommit))
})
output$text1 <- renderText({
paste("Input cred hist is :",randpred())
})