【发布时间】:2021-05-09 05:04:35
【问题描述】:
我正在尝试模拟评级量表的数据,然后让数据呈现闪亮。我尝试了我能想到的所有可能的解决方案 2 天,但无法弄清楚...... 我相信是我制作的函数的“杂乱”编程导致了它。具体来说,在创建列名时没有使用“输入”因变量,但对此非常不确定,甚至更多关于如何解决这个问题。
它应该包含在一个更广泛的程序中,该程序将为人口统计和度量创建各种表格,然后根据它们创建输出。
library(tidyverse)
library(lme4)
library(PearsonDS)
sim_CAINS <- function(su_n,hc_n) {
CAINS <- tibble(
sub_id = numeric(),
CAINS_Motivationfam =numeric(),
CAINS_Motivationsoc =numeric(),
CAINS_Pleasure_Social_lastweek =numeric(),
CAINS_Pleasure_Social_nextweek =numeric(),
CAINS_Motivation_Work =numeric(),
CAINS_Pleasureworknextweek =numeric(),
CAINS_Motivationrec =numeric(),
CAINS_Pleasurereclastweek =numeric(),
CAINS_Pleasurerecnextweek =numeric(),
CAINS_Expressionfac =numeric(),
CAINS_Expressionvoc =numeric(),
CAINS_Expressiongest =numeric(),
CAINS_Expressionspeechquant =numeric())
##sampling for service users
for ( id in 1:su_n) {
CAINS <- CAINS %>% add_row(
sub_id = id,
CAINS_Motivationfam = round(rpearson(1, moments = c(1.27, 1.09, .49,3))),
CAINS_Motivationsoc = round(rpearson(1, moments = c(1.67, 1.24, .33,3))),
CAINS_Pleasure_Social_lastweek = round(rpearson(1, moments = c(1.52, 1.43, .33,3))),
CAINS_Pleasure_Social_nextweek = round(rpearson(1, moments = c(1.44, 1.45, .40,3))),
CAINS_Motivation_Work = round(rpearson(1, moments = c(2.03, 1.44, .02,3))),
CAINS_Pleasureworknextweek = round(rpearson(1, moments = c(2.80, 1.29, -.86,3))),
CAINS_Motivationrec = round(rpearson(1, moments = c(1.03, 1.02, .72,3))),
CAINS_Pleasurereclastweek = round(rpearson(1, moments = c(1.50, 1.03, .19,3))),
CAINS_Pleasurerecnextweek = round(rpearson(1, moments = c(.90, 1.26, 1.07,3))),
CAINS_Expressionfac = round(rpearson(1, moments = c(1.56, 1.10, .31,3))),
CAINS_Expressionvoc = round(rpearson(1, moments = c(1.10, 1.13, .71,3))),
CAINS_Expressiongest = round(rpearson(1, moments = c(1.52, 1.25, .34,3))),
CAINS_Expressionspeechquant = round(rpearson(1, moments = c(0.75, .93, 1.03,3))))
}
##replace values that are too small or too big
CAINS[2:14] <- CAINS[2:14] %>% replace(CAINS[2:14]<0,0) %>% replace(CAINS[2:14]>4,4)
##sampling for healthy controls
for ( id in (1+su_n):(su_n+hc_n)) {
CAINS <- CAINS %>% add_row(
sub_id = id,
CAINS_Motivationfam = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Motivationsoc = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Pleasure_Social_lastweek = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Pleasure_Social_nextweek = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Motivation_Work = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Pleasureworknextweek = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Motivationrec = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Pleasurereclastweek = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Pleasurerecnextweek = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Expressionfac = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Expressionvoc = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Expressiongest = sample(0:2,1,F,c(.7,.2,.1)),
CAINS_Expressionspeechquant = sample(0:2,1,F,c(.7,.2,.1)))
}
return(CAINS)
}
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Negative Symptom Simulator"),
# Sidebar with a number input for service users and healthy controls
sidebarLayout(
sidebarPanel(
h3("Participant Numbers"),
numericInput("nSU", label = h5("Service Users"), value = 20),
numericInput("nHC", label = h5("Healthy Controls"), value = 20),
actionButton("update", "Update")
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("CAINS", plotOutput("tableCAINS"))
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
storage <- reactiveValues()
observeEvent(input$update, {
observe({
storage$dataCAINS <-sim_CAINS(input$nSU,input$nHC)
})
})
output$tableCAINS <- renderTable(storage$dataCAINS)
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】: