-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.R
141 lines (137 loc) · 7.41 KB
/
server.R
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
library(shiny)
library(ggplot2)
library(reshape2)
require(googleVis)
shinyServer(function(input, output) {
pop<- reactive({sample(1:20, input$population, replace = TRUE)})
bootstrapSample<-reactive({sample(pop(),input$sampleSize*input$numSample,replace = TRUE)})
popVar<- reactive({round(var(pop()),2)})
output$population <- renderText({input$population})
output$numSample <- renderText({input$numSample})
output$sampleSize <- renderText({input$sampleSize})
output$popVar <- renderText({popVar()})
output$biaVar <- renderText({
sample<- as.data.frame(matrix(bootstrapSample(), nrow = input$numSample,ncol =input$sampleSize))
return(round(mean(rowSums((sample-rowMeans(sample))^2)/input$sampleSize), 2))
})
output$unbiaVar <- renderText({
sample<- as.data.frame(matrix(bootstrapSample(), nrow = input$numSample,ncol =input$sampleSize))
return(round(mean(rowSums((sample-rowMeans(sample))^2)/(input$sampleSize-1)),2))
})
output$popHist <- renderGvis({
popHist <- gvisHistogram(data.frame(pop()), options = list(
height = "300px",
legend = "{position: 'none'}", title = "Population Distribution",
subtitle = "samples randomly drawn (with replacement) from values 1 to 20",
histogram = "{ hideBucketItems: true, bucketSize: 2 }",
hAxis = "{ title: 'Values', maxAlternation: 1, showTextEvery: 1}",
vAxis = "{ title: 'Frequency'}"
))
return(popHist)
})
output$sampBiaVarHist <- renderGvis({
sample<- as.data.frame(matrix(bootstrapSample(), nrow = input$numSample,ncol =input$sampleSize))
estVar <- data.frame(estVar = rowSums((sample-rowMeans(sample))^2)/length(sample))
popHist <- gvisHistogram(estVar, options = list(
height = "200px",
legend = "{position: 'none'}",
title = paste("Distribution of Biased Sample Variances, mean =", toString(round(mean(estVar$estVar),2))),
subtitle = "samples randomly drawn (with replacement) from values 1 to 20",
histogram = "{ hideBucketItems: true, bucketSize: 2 }",
hAxis = "{ title: 'Values', maxAlternation: 1, showTextEvery: 3, minValue: 0, maxValue: 80}",
vAxis = "{ title: 'Frequency'}"
))
return(popHist)
})
output$sampUnbiaVarHist <- renderGvis({
sample<- as.data.frame(matrix(bootstrapSample(), nrow = input$numSample,ncol =input$sampleSize))
estVar <- data.frame(estVar = rowSums((sample-rowMeans(sample))^2)/(length(sample)-1))
popHist <- gvisHistogram(estVar, options = list(
height = "200px",
legend = "{position: 'none'}",
title = paste("Distribution of Unbiased Sample Variances, mean =", toString(round(mean(estVar$estVar),2))),
subtitle = "samples randomly drawn (with replacement) from values 1 to 20",
histogram = "{ hideBucketItems: true, bucketSize: 2 }",
hAxis = "{ title: 'Values', maxAlternation: 1, showTextEvery: 3, minValue: 0, maxValue: 80}",
vAxis = "{ title: 'Frequency'}"
))
return(popHist)
})
output$varPlot <- renderPlot({
sample<- as.data.frame(matrix(bootstrapSample(), nrow = input$numSample,ncol =input$sampleSize))
estVar <- data.frame(estVar = rowSums(
(sample-rowMeans(sample))^2)/input$sampleSize)
difference <- estVar - var(pop())
difference <- cbind(index = 1:nrow(difference), difference)
varPlot <- ggplot(data = difference, aes(x = index, y = estVar)) +
geom_point() +
geom_hline(yintercept=0, col = "red", size = 2) + geom_smooth() +
ggtitle("Difference Between Population and Biased Sample Variances") +
labs(x = "Sample", y = "Biased Variance - Population Variance")
varPlot
})
output$unbiaVarPlot <- renderPlot({
sample<- as.data.frame(matrix(bootstrapSample(), nrow = input$numSample,ncol =input$sampleSize))
estVar <- data.frame(estVar = rowSums((sample-rowMeans(sample))^2)/(input$sampleSize-1))
difference <- estVar - var(pop())
difference <- cbind(index = 1:nrow(difference), difference)
varPlot <- ggplot(data = difference, aes(x = index, y = estVar)) +
geom_point() +
geom_hline(yintercept=0, col = "red", size = 2) + geom_smooth() +
ggtitle("Difference Between Population and Unbiased Sample Variances") +
labs(x = "Sample", y = "Unbiased Variance - Population Variance")
varPlot
})
output$varDiffPlot <- renderGvis({
sample<- as.data.frame(matrix(bootstrapSample(), nrow = input$numSample,ncol =input$sampleSize))
result <- data.frame(size = numeric(0), meanVar = numeric(0))
suppressWarnings(data <- melt(sample)$value)
for(n in 2:input$sampleSize){
row <- floor(length(data)/n)
tempData <- data[1:(row*n)]
dim(tempData) <- c(row, n)
result[n-1, ] <- c(n, round(mean(
rowSums((tempData-rowMeans(tempData))^2)/n)/popVar(), 2))
}
hAxisLabel <- paste(2:input$sampleSize, collapse = ",")
hAxisOptionsStr <- paste(
"{ title: 'Sample Size', gridlines : {count: -1}, minValue: 2, ticks: [",
hAxisLabel, "]}", sep = "")
varDiffPlot <- gvisColumnChart(result, xvar = "size", yvar = "meanVar",
options = list(
height = "200px",
legend = "{position: 'none'}",
title = "Mean Biased Sample Variance vs Sample Size",
hAxis = hAxisOptionsStr,
vAxis = "{ title: 'Percent of Population Variance \\ (%)',
minValue: 0, format: '##%' }"
))
return(varDiffPlot)
})
output$unbiaVarDiffPlot <- renderGvis({
sample<- as.data.frame(matrix(bootstrapSample(), nrow = input$numSample,ncol =input$sampleSize))
result <- data.frame(size = numeric(0), meanVar = numeric(0))
suppressWarnings(data <- melt(sample)$value)
for(n in 2:input$sampleSize){
row <- floor(length(data)/n)
tempData <- data[1:(row*n)]
dim(tempData) <- c(row, n)
result[n-1, ] <- c(n, round(mean(
rowSums((tempData-rowMeans(tempData))^2)/(n-1))/popVar(), 2))
}
hAxisLabel <- paste(2:input$sampleSize, collapse = ",")
hAxisOptionsStr <- paste(
"{ title: 'Sample Size', gridlines : {count: -1}, minValue: 2, ticks: [",
hAxisLabel, "]}", sep = "")
varDiffPlot <- gvisColumnChart(result, xvar = "size", yvar = "meanVar",
options = list(
height = "200px",
legend = "{position: 'none'}",
title = "Mean Unbiased Sample Variance vs Sample Size",
hAxis = hAxisOptionsStr,
vAxis = "{ title: 'Percent of Population Variance \\ (%)',
minValue: 0, format: '##%' }"
))
return(varDiffPlot)
})
})