-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombined.R
More file actions
1188 lines (959 loc) · 45.7 KB
/
combined.R
File metadata and controls
1188 lines (959 loc) · 45.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#combine ui and server
#Libraries
library(markdown)
library(ggplot2)
library(shinydashboard)
library(ggmosaic)
library(datasets)
library(shinythemes)
library(shiny)
library(MASS)
library(RColorBrewer)
library(grid)
library(gridExtra)
library(shiny)
library(shinydashboard)
library(LaplacesDemon)
shinyApp(
# Create the UI with a dashboard for the project.
# The dashboard will provide a space to add and delete desired panels.
ui = dashboardPage(
dashboardHeader(),
#The sidebar will be where the selection of the groups will occur.
# Using checkboxGroupInput will simpify the code for choosing which tabs we want.
dashboardSidebar(
checkboxGroupInput("checkGroup", label = h3("Topic Selction"),
choices = list("One Proportion" = 1, "Confidence Interval" = 2, "One Mean" = 3,
"Two Proportions" = 4, "Two Means" = 5, "Correlation" = 6, "Outliers" = 7,
"Equation" = 8, "ANOVA Means" = 9, "ANOVA St Dev" = 10, "ANOVA Sample Size" = 11))
),
# This code will allow the tab selections we create to appear in the output.
dashboardBody(
uiOutput('mytabs')
)
),
server = function(input, output, session){
# Here we build the UI for each tab panel. Since we are building the UI based on input,
# this infomration goes into the renderUI() function
output$mytabs = renderUI({
# Create a list of all possible tabs to be selected. We will select from this list of panels
# based on out checkBoxInput. The selected will correspond to these panels,
# giving us the new collection of panels to build out UI output.
newTabPanels <- list(
# One Proportion Sampling Distribution
tabPanel(h6("One Proportion"),
fixedPage(responsive = NULL,
column(width = 2,
numericInput("popProp", "Population Proportion",
value = 0.3,min = 0, max = 1, step = 0.01),
numericInput("sampleSize", "Sample Size", value = 100, min = 1),
radioButtons("numSamp", label = "Number of Samples",
choices = list("1 " = 1, "100" = 100, "Many" = 1000),
selected = 1),
actionButton("goProp", "Draw",class="btn btn-success btn")
),
column(width = 9,
verbatimTextOutput("sdist"),
tableOutput("sampSumDat"),
plotOutput("sampleDist", width = "auto", height = 200),
verbatimTextOutput("singdist"),
tableOutput("popSumDat"),
plotOutput("samplingDist", width = "auto", height = 200)
)
)),
#Confidence interval
tabPanel(h6("Confidence Interval"),
fluidRow(
box(width = 3, title = NULL, status = "primary",
sliderInput("cIDemoCL", "Confidence Level %", value = 80, min = 80, max = 99, step = 5)
),
box(width = 3, title = NULL, status = "primary",
sliderInput("cIDemoSampSize", "Sample Size", value = 25, min = 25, max = 500, step = 5)
),
box(width = 3, title = NULL, status = "primary",
sliderInput("cIDemoNumSamp", "Number of Samples", value = 25, min = 1, max = 100, step = 24)
),
box(width = 3, status = "primary",
actionButton("redrawCI", h5("Draw"), width = '100%',class="btn btn-success btn")
)
),
fluidRow(
box(width = 12, title = NULL, status = "primary",
plotOutput("sampCLPlot"))
)
),
# Inference for one Mean
tabPanel( h6("One Mean"),
fixedPage(responsive = NULL,
column(width = 2,
numericInput("popMeanOM", "Population Mean",
value = 10,min = 0, max = 1000, step = 1),
numericInput( "sigma", "Standard Deviation", value = 1, min = 0.01),
numericInput("sampleSizeOM", "Sample Size", value = 10, min = 1),
radioButtons("numSampOM", label = "Number of Samples",
choices = list("1 " = 1, "100" = 100, "Many" = 1000),
selected = 1),
actionButton("goMean", "Draw",class="btn btn-success btn")
),
column(width=8,
verbatimTextOutput("OneMeanDist"),
tableOutput("sampSumDatOM"),
plotOutput("sampleDistOM", width = "auto", height = 200),
verbatimTextOutput("ManyMeansDist"),
plotOutput("samplingDistOM", width = "auto", height = 200),
tableOutput("popSumDatOM")
)
)),
# Inference for two Proportions
tabPanel( h6("Two Proportions"),
fixedPage( responsive = NULL,
column(width = 3,
box( width ='100%', status = "primary", title = "Group 1",
numericInput("popPropG1P", h6("Population Proportion "),
value = 0.3, min = 0, max = 1, step = 0.01),
numericInput("sampleSizeGP1", h6("Sample Size"), value = 100, min = 1)
),
box(width = "100%", status = "primary", title = "Group 2",
numericInput("popPropG2P",h6("Population Proportion "),
value = 0.3, min = 0, max = 1, step = 0.01),
numericInput("sampleSizeGP2", h6("Sample Size "), value = 100, min = 1)
),
radioButtons("numSampGP", label = "Number of Samples",
choices = list("1 " = 1, "100" = 100, "Many" = 1000),
selected = 1),
actionButton("go2Prop", "Draw",class="btn btn-success btn")
),
column(width = 9,
tableOutput("sampSumDatP2"),
verbatimTextOutput("twoPropSampleDist"),
plotOutput("sampleDistG1", width = 'auto', height = 200),
tableOutput("popSumDat2P"),
verbatimTextOutput("twoPropSampDist"),
plotOutput("samplingDistTP", width = 'auto', height = 200)
)
)),
# Inference for two Means
tabPanel( h6("Two Means"),
fixedPage( responsive = NULL,
column(width = 3,
box(width = '100%', title = "Group 1", status = "primary",
numericInput("popMeanM1", h6("Mean"),
value = 5,min = 0, max = 1, step = 0.1),
numericInput( "sigmaTM1", h6("Standard Deviation"), value = 1, min = 0.01),
numericInput("sampleSizeTM1", h6("Sample Size"), value = 10, min = 1)
),
box(width = '100%', title = "Group 2", status = "primary",
numericInput("popMeanM2", h6("Mean"), value = 5,min = 0, max = 1, step = 0.1),
numericInput( "sigmaTM2", h6("Standard Deviation"), value = 1, min = 0.01),
numericInput("sampleSizeTM2", h6("Sample Size"), value = 10, min = 1)
),
radioButtons("numSampTM", label = "Number of Samples",
choices = list("1 " = 1, "100" = 100, "Many" = 1000),
selected = 1),
actionButton("go2Mean", "Draw",class="btn btn-success btn")
),
column(width = 8,
verbatimTextOutput("twoMeansSampDist"),
tableOutput("sampSumDatTM"),
plotOutput("sampleDistM1", width = 'auto', height = 300),
verbatimTextOutput("twoMeansSamplingDist"),
tableOutput("popSumDatTM"),
plotOutput("samplingDistTM", width = '100%', height = 200)
)
)
),
# Correlation
tabPanel( h6("Correlation"),
column(width = 12,
box(width = 12,
title = NULL, status= "primary",
sliderInput("correlation", "Correlation", value = 0.1, min = -1, max =1, step = 0.01)
),
box( width = 10, status = "primary", align = "center",
plotOutput("corrPlot", width = '350', height = '350')
))
),
# Outliers
tabPanel(h6("Outliers"),
fixedPage(
column(
width = 5, status= "primary",
actionButton("outData", "Plot Data"),
checkboxInput("fitLine", label = "Fit Line", value = FALSE),
verbatimTextOutput("lineSum"),
tableOutput("lineEq"),
sliderInput("outX", "Outlier X",
value = 0.3,min = -2.5, max = 2.5, step = 0.01),
sliderInput("outY", "Outlier Y",
value = 0.3,min = -2.5, max = 2.5, step = 0.01),
checkboxInput("fitLineNoPt", label = "Fit Line Without Point", value = FALSE),
verbatimTextOutput("lineSumNoPt"),
tableOutput("lineEqNoPt")
),
column(
width = 6, status = "primary",
box(
width = "100%", height = "100%",
title = "Linear Regression", status = "primary",
plotOutput("outlierPlot" )
)
))
),
#Regression tab
tabPanel(h6("Equation"), value = "eqBd",
fixedPage(responsive= NULL,
column(width = 4, status = "primary",
sliderInput("ssx", "St dev X", value = 15, min = 0, max =40),
sliderInput("ssy", "St dev y", value = 15, max =40, min = 0),
sliderInput("meanx", "Mean x", value = 10, max =20, min = -20),
sliderInput("meany", "Mean y", value = 10, min = -20, max =20),
sliderInput("correlationLM", "Correlation", value = 0.5, min = -1, max =1, step = 0.01)
# checkboxInput("fitPoints", "Fit Line",value = FALSE)
),
column(
checkboxInput("fitPoints", "Fit Line",value = FALSE),
width = 7, status = "primary",
box(
width = "100%", height = "100%",
title = "Linear Regression", status = "primary",
plotOutput("linreg" ),
tableOutput("eqPointsTable")
)
)
)),
#ANOVA Mean
tabPanel(h6("ANOVA Means"),
fixedPage(
column(width = 4,
sliderInput("anovaMean1", h6("Mean 1"), value = 30, min = 10, max =50, step = 1)
),
column(width = 4,
sliderInput("anovaMean2", h6("Mean 2"), value = 30, min = 10, max =50, step = 1)
),
column(width = 4,
sliderInput("anovaMean3", h6("Mean 3"), value = 30, min = 10, max =50, step = 1)
)
),
actionButton("goAnovaMean", "Draw",class="btn btn-success btn"),
fluidRow(
column(width = 7,
plotOutput("anovaPlotMean", width = "auto", height = 500)
),
column( width = 5,
tableOutput("anovaDatSumMean"),
tableOutput("anovaTableMean")
)
)
),
#ANOVA Standard deviation
tabPanel(h6("ANOVA St Dev"),
fixedPage(
column(width = 4,
sliderInput("anovaSD1", h6("Standard Deviation 1"), value = 10, min = 1, max =50, step = 1)
),
column(width = 4,
sliderInput("anovaSD2", h6("Standard Deviation 2"), value = 10, min = 1, max =50, step = 1)
),
column(width = 4,
sliderInput("anovaSD3", h6("Standard Deviation 3"), value = 10, min = 1, max =50, step = 1)
)
),
actionButton("goAnovaSD", "Draw",class="btn btn-success btn"),
fluidRow(
column(width = 7,
plotOutput("anovaPlotSD", width = "auto", height = 500)
),
column( width = 5,
tableOutput("anovaDatSumSD"),
tableOutput("anovaTableSD")
)
)
) ,
#ANOVA Sample Size
tabPanel(h6("ANOVA Sample Size"),
fluidRow(
column(width = 8,
sliderInput("sampSizeANOVA", h6("Sample Size"), value = 50, min = 10, max =100, step = 5)
),
column(width = 4,
actionButton("goAnovaSS", "Draw",class="btn btn-success btn", width = '100%')
),
fluidRow(
column(width = 7,
plotOutput("anovaPlotSS", width = "auto", height = 500)
),
column( width = 5,
tableOutput("anovaDatSumSS"),
tableOutput("anovaTableSS")
)
)
)
)
)
# Choose the new tabs based on the input from the user.
collectNewTabs = newTabPanels[c(as.numeric(input$checkGroup))]
# doCall creates the layout for the tabsetPanel based on the selection above
do.call(tabsetPanel, collectNewTabs)
})
# ------------ This section will contain all the server code that goes with each tab. ------------- #
#functions
binomProb = function(n, p){
out = rbinom(n,1,p)
propSuccess = round(length(which(out == 1))/n, 4)
return(propSuccess)
}
binfun = function(data,n){
bin = (3.5*sd(data) )/(n^(1/3))
return(bin)
}
# -------------------------------------------- Inference for One Proportion Sampling Dist. ----------------------------#
# Create a single sample distribution
# Get a sample from a binomial distribution
pickVect = eventReactive(input$goProp,{
pickf = binomProb(input$sampleSize,input$popProp)
return(pickf)
})
#Sample Distribution bar graph
output$sampleDist = renderPlot({
qplot(c(rep("Success", input$sampleSize*pickVect()),rep("Failure", input$sampleSize*(1-pickVect()))), xlab = "Category", ylab = "Count")
})
#Sample summary information to be put into a table
samplesum = eventReactive(input$goProp,{data.frame(
Category = c( "Success", "Failure"),
Prop = c(pickVect(), 1-pickVect())
)
})
#Sample summary table output
output$sampSumDat = renderTable(caption = "Sample Summary Statistics",caption.placement = getOption("xtable.caption.placement", "top"),{
samplesum()
})
#Calculations to make a sampling distribution
samples = eventReactive(input$goProp,{
# If one sample is needed use only the one p_hat from above
if(input$numSamp == 1){
return(pickVect())
}
# If many samples are needed, generate a vector of p_hats by the above method
if(input$numSamp > 1){
v = c(pickVect())
for(i in 2:input$numSamp){
# Generate a sample
npick = reactive({
newprop = binomProb(input$sampleSize, input$popProp)
return(newprop)
})
# Store the generated sample in a vector
v = c(v,npick())
}}
# Return to
return(v)
})
bins = reactive({
if(input$numSamp>1){
bin = (3.5*sd(samples()) )/(input$sampleSize^(1/3))}
else{ bin = 0.1}
return(bin)
})
# Histogram of the sampling distribution
output$samplingDist =renderPlot({
ggplot(data = data.frame(samples()),aes(samples()))+ geom_histogram(binwidth = bins()) +xlab("Sample Proportion") +ylab("Number of Samples")
})
# Population summary info to be displayed in a table
popSum = eventReactive(input$goProp,{data.frame(
Mean = mean(samples()),
"Standard Deviation" = sd(samples())
)
})
#Population summary table output
output$popSumDat = renderTable(caption = "Sampling Distribution Summary Statistics",caption.placement = getOption("xtable.caption.placement", "top"),{
popSum()
})
# Add the text for "Sample/Sampling Distribution" to the Shiny display
output$sdist <- renderText({ "Data Distribution" })
output$singdist <- renderText({ "Sampling Distribution" })
#--------------------------------------------- One proportion CIs ---------------------------------------------------- #
# Generate 20 sample p_hats to create confidence intervals around
twSamps = eventReactive(input$redrawCI,{
tsampCL = NULL
for(i in 1:input$cIDemoNumSamp){
# Get the proportion of "successes" for the generated sample
countPropCL = binomProb(input$cIDemoSampSize, 0.5)
tsampCL = c(tsampCL,countPropCL)
}
return(tsampCL)
})
# Create a sample vector of y values so that the confidence intervals can be stacked in the plot
ysCLDemo = reactive({
yy = seq(from = 1, by = 0.5, length.out = length(twSamps()))
return(yy)
})
# Set the z-value to be the value input by the user
zlev = reactive({
lev = qnorm(((100 - (100-input$cIDemoCL)/2)/100), 0,1)
return(lev)
})
# Calculate the upper bounds for the confidence intervals
upCLDemo = reactive({
usd = twSamps() + zlev()*(sqrt(twSamps()*(1-twSamps())/input$cIDemoSampSize))
return(usd)
})
# Calculate the lower bounds for the confidence intervals
lowCLDemo = reactive({
lsd = twSamps() - zlev()*(sqrt(twSamps()*(1-twSamps())/input$cIDemoSampSize))
return(lsd)
})
# Color the interval based on whether or not they capture the true population proportion
colorCLDemo = reactive({
coldemo = NULL
for(i in 1:input$cIDemoNumSamp){
# Color the intervals blue if they contain 0.4 (the set value for the population parameter)
if(lowCLDemo()[i] <= 0.5 && 0.5 <= upCLDemo()[i] ){
colne = "steelblue1"
coldemo = c(coldemo,colne)
}
# Color the intervals orange if they do not contain 0.4 (the set value for the population parameter)
else{
colne = "tan1"
coldemo = c(coldemo,colne)
}
}
# Return the vector of colors to be added to the plot
return(coldemo)
})
# Graph the confidence intervals stacked on one plot to display how many captured the population proportion
output$sampCLPlot = renderPlot({
qplot(x = twSamps(), y = ysCLDemo(), xlab = "Confidence Intervals")+xlim(0,1)+
geom_segment(aes(x = lowCLDemo(), xend = upCLDemo(), y = ysCLDemo(), yend = ysCLDemo()), colour = colorCLDemo())+
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank())+geom_vline(xintercept = 0.5, color = "slategray4")
})
# --------------------------------------------- Inference for One Mean ------------------------------------#
# Calculations to get a sample distribution
# Generate a sample of means from a population with the input mean and the sample standard deveation
pickOM = eventReactive(input$goMean,{
pickfOM = c(rnorm(input$sampleSizeOM, input$popMeanOM, input$sigma))
return(pickfOM)
})
# Sample Distribution output
output$sampleDistOM = renderPlot({
qplot(pickOM(), xlab = "x", ylab = "Count")
})
#Sample summary information to be displayed in a table
samplesumOM = reactive({data.frame(
Mean = mean(pickOM()),
"Standard Deviation" = sd(pickOM())
)
})
# Output for the sample summary information
output$sampSumDatOM = renderTable(caption = "Sample Summary Statistics",caption.placement = getOption("xtable.caption.placement", "top"),{
samplesumOM()
})
#Calculations to make a sampling distribution
# Generate many samples
samplesOM = eventReactive(input$goMean,{
# If the number of samples is only one just keep the one sample generated above
if(input$numSampOM == 1){
return(mean(pickOM()))
}
# If the user wants many samples generate them as follows
if(input$numSampOM > 1){
v = c(mean(pickOM()))
for(i in 2:input$numSampOM){
nnpick = reactive({
newpick = c(rnorm(input$sampleSizeOM, input$popMeanOM, input$sigma))
# Calculate the mean for your new sample
meanSample = mean(newpick)
return(meanSample)
})
# Store the mean for the sample in a vector
v = c(v,nnpick())
}
}
# Return the vector of the means of each sample generated
return(v)
})
binsOM = reactive({
if(input$numSampOM > 1){
bin = (3.5*sd(samplesOM()) )/(input$sampleSizeOM^(1/3))}
else{ bin = 0.5}
return(bin)
})
# Output a histogram of the sampling distribution of the sample means
output$samplingDistOM =renderPlot({
ggplot(data = data.frame(samplesOM()),aes(x = samplesOM())) + geom_histogram(binwidth = binsOM())+xlab("Sample Means") +ylab("Number of Samples")
})
# Population summary information to be put in a table
popSumOM = reactive({data.frame(
Mean = mean(samplesOM()),
"Standard Deviation" = sd(samplesOM())
)
})
# Output the table of the population summary information
output$popSumDatOM = renderTable(caption = "Summary of Sampling Distribution",caption.placement = getOption("xtable.caption.placement", "top"),{
popSumOM()
})
# Put text on the shiny app to distinguish the Sample Distribution from the Sampling Distribution
output$OneMeanDist <- renderText({ "Data Distribution" })
output$ManyMeansDist <- renderText({ "Sampling Distribution" })
#--------------------------------------------- Inference in Two Proportions ---------------------------------------#
# Calculations to get a sample distribution for group 1
pickP1Prop = eventReactive(input$go2Prop,{
pickfp1 = binomProb(input$sampleSizeGP1, input$popPropG1P)
return(pickfp1)
})
# Generate a binomial sample for group 2
pickP2Prop = eventReactive(input$go2Prop,{
pickfp2 =binomProb(input$sampleSizeGP2, input$popPropG2P)
return(pickfp2)
})
propDat = reactive({data.frame(
sf = c(rep("Success", input$sampleSizeGP1*pickP1Prop()),rep("Failure", input$sampleSizeGP1*(1-pickP1Prop())),
rep("Success", input$sampleSizeGP2*pickP2Prop()),rep("Failure", input$sampleSizeGP2*(1-pickP2Prop()))),
grp = c(rep("Group 1", input$sampleSizeGP1), rep("Group 2", input$sampleSizeGP2) )
)
})
# Sample Distribution output for group 1 sample
output$sampleDistG1 = renderPlot({
ggplot(data = propDat()) + geom_mosaic(aes(x = product(sf,grp), fill = as.factor(sf)))+xlab("Group")+
guides(fill=guide_legend(title="Category"))
})
#Sample summary information for two proportions to be displyed in a table
samplesumP2 = eventReactive(input$go2Prop,{data.frame(
"Proportion 1" = pickP1Prop(),
"Proporiton 2" = pickP2Prop(),
Difference =pickP1Prop()-pickP2Prop()
)
})
# Table output for the difference in the sample distributions
output$sampSumDatP2 = renderTable(caption = "Summary of Samples",caption.placement = getOption("xtable.caption.placement", "top"),{
samplesumP2()
})
#### Sampling Dist for two proportions ####
# Calculations to make a sampling distribution of the difference in proportions
samplesP2 = eventReactive(input$go2Prop,{
# If there is only one sample needed, use the one created above
if(input$numSampGP == 1){
return(pickP1Prop()-pickP2Prop())
}
# If many samples are needed, draw many samples for two groups
if(input$numSampGP > 1){
vp2 = c(pickP1Prop()-pickP2Prop())
for(i in 2:input$numSampGP){
npickdiffp = reactive({
# Calculate the difference in the two samples
newpropdiff = binomProb(input$sampleSizeGP1, input$popPropG1P) - binomProb(input$sampleSizeGP2, input$popPropG2P)
# Return the difference of the two samples
return(newpropdiff)
})
# Store differences in a vector
vp2 = c(vp2,npickdiffp())
}}
# Return the vector of differences
return(vp2)
})
binsTwoProp = reactive({
if(input$numSampGP > 1){
bin = (3.5*sd(samplesP2()) )/(input$sampleSizeGP1^(1/3))}
else{ bin = 0.1}
return(bin)
})
# Output the plot for the sampling distribution for the difference in proportions
output$samplingDistTP =renderPlot({
qplot(samplesP2(), xlab = "Difference in Proportions", ylab = "Number of Samples", binwidth = binsTwoProp())
})
# Population summary info and p-values to be displayed in a table
popSumTP = reactive({data.frame(
Mean = mean(samplesP2()),
"Standard Deviation" = sd(samplesP2())
)
})
# Output the table of summary information and test statistics
output$popSumDat2P = renderTable(caption = "Samping Distribution Summary",caption.placement = getOption("xtable.caption.placement", "top"),{
popSumTP()
})
# Output the text labels for the Distributions and test options
output$twoPropSampleDist <- renderText({ "Data Distribution" })
output$twoPropSampDist <- renderText({ "Sampling Distribution" })
# --------------------------------------------- Two Means Code -----------------------------------------------------#
# Generate a sample for group one
pickM1 = eventReactive(input$go2Mean,{
pickfM1 = c(rnorm(input$sampleSizeTM1, input$popMeanM1, input$sigmaTM1))
return(pickfM1)
})
# Generate a sample for group two
pickM2 = eventReactive(input$go2Mean,{
pickfM2 = c(rnorm(input$sampleSizeTM2, input$popMeanM2, input$sigmaTM2))
return(pickfM2)
})
# Make data frame
datTM = reactive({data.frame(
dataTM = c(pickM1(), pickM2()),
groupTM = c(rep("Group 1", input$sampleSizeTM1), rep("Group 2", input$sampleSizeTM2))
)
})
#Sample Distribution plot
output$sampleDistM1 = renderPlot({
ggplot(datTM(), aes(x=groupTM, y=dataTM, fill = groupTM)) +
geom_dotplot(binaxis='y', stackdir='center', dotsize = 0.5, binwidth = binsTM())+
stat_summary(fun.y=mean, geom="point", shape=5,size=6, color="black")+
labs(x = "Group", y = "Value")+
guides(fill=guide_legend(title="Group"))+scale_y_continuous(NULL, breaks = NULL)
})
# Table summary information for groups one and two
samplesumTM = reactive({data.frame(
Mean1 = mean(pickM1()),
Mean2 = mean(pickM2()),
Difference= mean(pickM1())-mean(pickM2())
)
})
# Output the table information for groups one and two
output$sampSumDatTM = renderTable(caption = "Summary of Samples",caption.placement = getOption("xtable.caption.placement", "top"),{
samplesumTM()
})
# Generate many samples to make a sampling distribution of differences in means
samplesTM = eventReactive(input$go2Mean,{
# For only one sample keep the difference from above
if(input$numSampTM == 1){
return(mean(pickM1())-mean(pickM2()))
}
# For many samples, generate many samples, calculate the means, and get the difference in the means
if(input$numSampTM > 1){
vm = c(mean(pickM1())-mean(pickM2()))
for(i in 2:input$numSampTM){
nmpick = reactive({
diffmean = mean(c(rnorm(input$sampleSizeTM1, input$popMeanM1, input$sigmaTM1))) - mean(c(rnorm(input$sampleSizeTM2, input$popMeanM2, input$sigmaTM2)))
return(diffmean)
})
# Hold all the differences in means in a vector
vm = c(vm,nmpick())
}
}
# Return the differences in means vector to be plot as the sampling distribution
return(vm)
})
binsTM = reactive({
if(input$numSampTM > 1){
bin = (3.5*sd(samplesTM()) )/(input$sampleSizeTM1^(1/3))}
else{ bin = 0.5}
return(bin)
})
# Plot the sampling distribution for the differences in means
output$samplingDistTM =renderPlot({
qplot(samplesTM(), xlab = "Value", ylab = "Count", binwidth = binsTM())
})
# Sampling distribution and test statistics information to be displayed in a table
popSumTM = reactive({data.frame(
Mean = mean(samplesTM()),
"Standard Deviation" = sd(samplesTM())
)
})
# Table output of the above information
output$popSumDatTM = renderTable(caption = "Summary Sampling Distribution",caption.placement = getOption("xtable.caption.placement", "top"),{
popSumTM()
})
# Text output to label the distributions and test options
output$twoMeansSampDist <- renderText({ "Data Distribution" })
output$twoMeansSamplingDist <- renderText({ "Sampling Distribution" })
#---------------------------------------------- Correlation Tab ---------------------------------------------------#
# Make the dataset with a set correlation for the scatterplot
xycorr =reactive({
datxy = as.data.frame(mvrnorm(100, mu = c(0,0), Sigma = matrix(c(1,input$correlation,input$correlation,1),, ncol = 2),empirical = TRUE))
return(datxy)
})
#Output the scatterplot of the data created above
output$corrPlot = renderPlot({
qplot(xycorr()$V1,xycorr()$V2, xlab = "x", ylab = "y")
})
#----------------------------------------------- Outliers tab ----------------------------------------------------#
getData = eventReactive(input$outData,{
datxy = as.data.frame(mvrnorm(50, mu = c(0,0),
Sigma = matrix(c(1,0.99,0.99,1),, ncol = 2),
empirical = TRUE))
datnew = data.frame(dataX = c(datxy$V1), dataY = c(datxy$V2))
return(datnew)
})
addOutlier = reactive({
datnew = data.frame(dataX = c(getData()$dataX, input$outX), dataY = c(getData()$dataY, input$outY))
return(datnew)
})
# Print the plot for the high leverage data
output$outlierPlot = renderPlot({
# Print the plot with all the data
print(ggplot(data = addOutlier(), aes(x = dataX, dataY)) + geom_point() +
geom_point(aes(x = dataX[51], y = dataY[51]), color= "firebrick2", size = 2))
# Print the plot with the line fit to the data
if(input$fitLine == TRUE){
# Make a linear model if the dataset
hL1 = lm(addOutlier()$dataY~addOutlier()$dataX)
# Print the plot with the fitted line
# Collect the information about the line to be displayed in a table
hL1Line = reactive({data.frame(
intercept = hL1$coefficients[1],
slope = hL1$coefficients[2],
Rsquared = summary(hL1)$r.squared
)
})
# Output the table of information about the line
output$lineEq = renderTable({
hL1Line()
})
print(ggplot(data = addOutlier(), aes(x = dataX, dataY)) + geom_point() +
geom_point(aes(x = dataX[51], y = dataY[51]), colour= "firebrick4", size = 2) +
geom_abline(slope = hL1$coefficients[2],intercept = hL1$coefficients[1], linetype = "longdash", colour="navyblue", size = 1))
}
# If the Fit with no red point box is selected
if(input$fitLineNoPt == TRUE ){
# Calculate the line of best fit without the point of interest
hL1NoPt = lm(addOutlier()$dataY[-51]~addOutlier()$dataX[-51])
# Print the plot with the line of fit for the data without the point of interest
print(ggplot(data = addOutlier(), aes(x = dataX, dataY)) + geom_point() +
geom_point(aes(x = dataX[51], y = dataY[51]), colour= "firebrick4", size = 2) +
geom_abline(slope = hL1NoPt$coefficients[2],intercept = hL1NoPt$coefficients[1], linetype = "dashed", colour="tan1", size = 1))
# Create a table of information about the line fit to the data without the point of interest
hL1LineNoPt = reactive({data.frame(
intercept = hL1NoPt$coefficients[1],
slope = hL1NoPt$coefficients[2],
Rsquared = summary(hL1NoPt)$r.squared
)
})
# Output this table to the user
output$lineEqNoPt = renderTable({
hL1LineNoPt()
})
# If the "Fit Line" is also selected, then the plot will display both lines of best fit
if(input$fitLine == TRUE){
print(ggplot(data = addOutlier(), aes(x = dataX, dataY)) + geom_point() +
geom_point(aes(x = dataX[51], y = dataY[51]), colour= "firebrick4", size = 2) +
geom_abline(slope = hL1$coefficients[2],intercept = hL1$coefficients[1], colour = "navyblue", linetype ="F1", size = 0.75) +
geom_abline(slope = hL1NoPt$coefficients[2],intercept = hL1NoPt$coefficients[1], linetype = "dashed", colour="tan1", size = 1)+
scale_colour_manual(name="Legend",values=c("fitted" = "navyblue", "noPoint" = "springgreen2"))+
scale_linetype_manual(name = "Legend", values = c("fitted" = "F1", "noPoint"= "dashed"))+
theme(legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"))+
guides(fill=guide_legend(title="Legend")))
}
}
})
# Add text to label the tables for the line with the point of interest and without it
output$lineSum <- renderText({ "Line Summary" })
output$lineSumNoPt <- renderText({ "Line Summary Without Point" })
# ------------------------------------------------ Regression Line -----------------------------------------------#
# Create a data frame with the desired slope
xy = reactive({
datXY = as.data.frame(mvrnorm(100, mu = c(0,0), Sigma = matrix(c((input$ssx)^2,input$correlationLM*input$ssx*input$ssy,input$correlationLM*input$ssx*input$ssy,(input$ssy)^2),, ncol = 2),empirical = TRUE))
normY = (datXY$V2 - mean(datXY$V2) / sd(datXY$V2)) + input$meany
normX = (datXY$V1 - mean(datXY$V1) / sd(datXY$V1)) + input$meanx
normdat = data.frame( xn = normX, yn = normY)
return(normdat)
})
# Change the intercept to match the one desired
linexy =reactive({
return(lm(xy()$xn~xy()$yn))
})
# Plot the points on the graph
output$linreg = renderPlot({
print(ggplot(data = xy(),aes(x = xy()$xn, y = xy()$yn)) + geom_point() + xlab("Explanatory") + ylab("Response")+
scale_x_continuous(limits = c(-100, 100))+scale_y_continuous(limits = c(-100, 100)))
# Add the line of best fit and display the equation in table form
if(input$fitPoints == TRUE){
pointsLine = lm(xy()$xn~xy()$yn)
print(ggplot(data = xy(),aes(x = xy()$xn, y = xy()$yn)) + geom_point()+xlab("Explanatory")+ylab("Response")+
geom_abline(intercept =pointsLine$coefficients[1], slope = pointsLine$coefficients[2])+
scale_x_continuous(limits = c(-100, 100))+scale_y_continuous(limits = c(-100, 100)))
# Make a table with the equation information
eqPoints= reactive({data.frame(
intercept = pointsLine$coefficients[1],
slope = pointsLine$coefficients[2] )
})
output$eqPointsTable = renderTable({
eqPoints()
})
}
})
# ----------------------------------------- ANOVA Mean ------------------------------------------------- #
# Generate data for 3 different groups based on the input means, standard deviations, and sample sizes
group1Data = eventReactive(input$goAnovaMean, {
dat1 = rnorm(50, input$anovaMean1,10)
return(dat1)
})
group2Data = eventReactive(input$goAnovaMean, {
dat2 = rnorm(50, input$anovaMean2,10)
return(dat2)
})
group3Data = eventReactive(input$goAnovaMean,{
dat3 = rnorm(50, input$anovaMean3, 10)
return(dat3)
})
# Combine all the data for the 3 groups
g123Data = eventReactive(input$goAnovaMean, {
alData = c(group1Data(), group2Data(), group3Data())
return(alData)
})
# Make a vector of the categories that each data point is in
groupList = eventReactive(input$goAnovaMean, {
grpList = c(rep("Group 1", 50), rep("Group 2", 50), rep("Group 3", 50))
return(grpList)
})
# Create a data frame all the data and the corresponding groups the data point goes in
dframe = reactive( {
newframe = data.frame(g123Data(), groupList())
return(newframe)
})
# Dotplot of the threee groups of data colored and faceted by group
output$anovaPlotMean = renderPlot( {
ggplot(dframe(), aes(x=groupList(), y=g123Data(), fill = groupList())) +
geom_dotplot(binaxis='y', stackdir='center', dotsize = 0.5,binwidth = 1)+
stat_summary(fun.y=mean, geom="point", shape=5,size=6, color="black")+
labs(x = "Group", y = "Value")+
guides(fill=guide_legend(title="Group"))+scale_y_continuous(NULL, breaks = NULL)
})
# Make a linear model based on the data in order to do an ANOVA analysis
mod=reactive({
ll = lm(g123Data()~groupList())
return(ll)
})