Guest User

Untitled

a guest
Jul 16th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. library(shiny)
  2. library(dplyr)
  3. library(tidyr)
  4. library(ggplot2)
  5. library(exact2x2)
  6. library(shinythemes)
  7. library(DT)
  8.  
  9.  
  10.  
  11.  
  12.  
  13. fet <- function(a,b,c,d) {round(fisher.test(matrix(c(a,b,c,d), byrow=T, nrow=2))$p.value,3)}
  14.  
  15. n1<-n2<-16
  16.  
  17. matrix_out<- matrix("NA",n1+1,n2+1)
  18. for (i in 0:n1){
  19. for (j in 0:n2){
  20. matrix_out[i+1,j+1] <- fet(i,n1-i,j,n2-j)
  21.  
  22. }
  23. }
  24. matrix_out<-as.data.frame(matrix_out)
  25.  
  26.  
  27.  
  28.  
  29.  
  30. ui <- fluidPage(
  31.  
  32. theme = shinytheme("united"),
  33.  
  34. titlePanel("Power and Sample Size Calculation",windowTitle = "Power/SampleSize"),
  35. hr(),
  36. h4("This is a simplified calculator; for now we will assume 2 groups only.
  37. Also, we will come back to time-to-event"),
  38. br(),br(),
  39. h5("If you have a set sample size and want to know the power,
  40. choose 'Power' tab, otherwise choose 'SampleSize' tab"),
  41.  
  42. sidebarLayout(
  43.  
  44. sidebarPanel(width = 5,
  45.  
  46. wellPanel(
  47. h5("Set your expected proportion of outcome (yes; infected; dead)"),
  48. br(),
  49. h5("in the Control Group:"),
  50. numericInput(inputId = "p1",
  51. label = "p1",
  52. value=0.2,
  53. min=0.0001,max=1),
  54.  
  55. br(),
  56. h5("in the Experimental Group:"),
  57. numericInput(inputId = "p2",
  58. label = "p2",
  59. value=0.8,
  60. min=0.0001,max=1)
  61. ),
  62. hr(),
  63. hr(),
  64.  
  65. conditionalPanel("input.tabselected=='Power'",
  66. h5("Set sample size in each group (enter an integer between 0 and 16):"),
  67. br(),
  68. h5("in the Control Group:"),
  69. numericInput(inputId = "n1",
  70. label = "n1",
  71. value=5,
  72. min=1,max=16),
  73.  
  74. br(),
  75. h5("in the Experimental Group:"),
  76. numericInput(inputId = "n2",
  77. label = "n2",
  78. value=5,
  79. min=1,max=16)
  80. ),
  81.  
  82. conditionalPanel("input.tabselected=='SampleSize'",
  83. h5("Set the power (enter a number between 0 and 1):"),
  84. br(),
  85. numericInput(inputId = "power",
  86. label = "Power",
  87. value=0.9,
  88. min=0, max=1)
  89. ),
  90. conditionalPanel("input.tabselected=='P-value Table'",
  91. h5("Set sample size in each group (enter an integer between 0 and 16):"),
  92. br(),
  93. h5("in the Control Group:"),
  94. numericInput(inputId = "n1_",
  95. label = "n1",
  96. value=2,
  97. min=1,max=16),
  98.  
  99. br(),
  100. h5("in the Experimental Group:"),
  101. numericInput(inputId = "n2_",
  102. label = "n2",
  103. value=2,
  104. min=1,max=16)
  105. )
  106. ),
  107.  
  108. mainPanel( width=7,
  109. tabsetPanel(
  110. id = "tabselected",
  111. tabPanel("Power",
  112. br(),
  113. br(),
  114. textOutput("output_power")),
  115. tabPanel("SampleSize",
  116. br(),
  117. textOutput("output_samplesize"),
  118. br(),
  119. br(),
  120. plotOutput("output_hist")),
  121. tabPanel("P-value Table",
  122. DT::dataTableOutput("output_table"),
  123. br(),
  124. br(),
  125. textOutput("des"))
  126.  
  127. )
  128. )
  129. )
  130. )
  131.  
  132.  
  133.  
  134.  
  135.  
  136. server <- function(input, output) {
  137.  
  138. ### Power tab
  139. output$output_power<-renderText({
  140. paste("Power =",round(power2x2(input$p1,input$p2,input$n1,input$n2)$power,3))
  141. })
  142.  
  143.  
  144. ### SampleSize tab and histogram
  145. output$output_samplesize<-renderText({
  146. paste("Total Sample Size per Group =",ss2x2(input$p1,input$p2,power=input$power)$n0)
  147. })
  148. myTab<-reactive({
  149. out <- matrix("NA",input$n1+1,input$n2+1)
  150. for (i in 0:input$n1){
  151. for (j in 0:input$n2){
  152. out[i+1,j+1] <- fet(i,input$n1-i,j,input$n2-j)
  153.  
  154. }
  155. }
  156. n <- 10000
  157. temp <- rep(out, round(n*t(t(dbinom(0:input$n1,input$n1,input$p1))) %*% dbinom(0:input$n2,input$n2,input$p2)))
  158. })
  159. output$output_hist<-renderPlot({
  160. hist(as.numeric(myTab()),breaks=20, xlab="Distribution of P-values", probability=T,yaxt="n",
  161. main="This is a histogram of the distributino of \n p-values you might get \n the line shows p=.05",ylab="")
  162. abline(v=.05, col="red")
  163. mtext(paste(round(mean(myTab()<=.05),3)," | ",round(mean(myTab()>.05),3)), side=3,adj=0,col="red")
  164. mtext("0.05", side=1,at=.05, col="red")
  165. })
  166.  
  167.  
  168. ### P-value table tab
  169. output$output_table<-DT::renderDataTable({
  170.  
  171. matrix_out_subset<-matrix_out[1:(input$n1_+1),1:(input$n2_+1)]
  172. colnames(matrix_out_subset) <- paste(0:input$n2_,"/",input$n2_," (",round(dbinom(0:input$n2_,input$n2_,input$p2),3),")",sep="")
  173. rownames(matrix_out_subset) <- paste(0:input$n1_,"/",input$n1_," (",round(dbinom(0:input$n1_,input$n1_,input$p1),3),")",sep="")
  174.  
  175. DT::datatable(data=matrix_out_subset,options=list(pageLength =20))
  176.  
  177. })
  178. output$des<-renderText({
  179. paste("The table above shows the possible outcomes for group 1 as the rows (n1=",input$n1_,"),and for group 2 as the columns (n2=",input$n2_,". For each row/comlumn,
  180. the probability of that outcome based on the probabilities that were specified
  181. are also given in the row/column headings. The data in the table represent
  182. the p-values that you would get for that combination of outcomes given by the row and column total.
  183. ")
  184. })
  185.  
  186.  
  187. }
  188.  
  189.  
  190. shinyApp(ui = ui, server = server)
Add Comment
Please, Sign In to add comment