-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_Product_Project.Rpres
95 lines (71 loc) · 3.17 KB
/
Data_Product_Project.Rpres
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
Data set mean guessing game
========================================================
author: Attila Vamos
date: 02/10/2016
URL to Shiny application:
https://attila-vamos.shinyapps.io/Data_Product_Project/
The aims of the Shiny application
========================================================
The main aim behind this application was to learn how to develop Shiny application, how to create, use and update input elements and how to handle input data in server code.
The Data set mean guessing game interactively displays a histogram of randomly generated standard deviation data and provide controls to the user to:
- guess the mean with a slider
- checking the guessed value
- generate a new data set and histogram to a game
Guessed value display and checking
========================================================
The guessed value is interactively (controlled by the slider) displayed on the histogram with a red vertical line
User can check the guess with a click on the "Check my guess" button and the app will:
- show the real mean with a blue vertical line
- display the real value in a text box
- calculate and display the MSE (Mean Square Error) value with the guessed and real mean
Generating a new data set and enabling a new guess
========================================================
To generate a new data set and histogram for the next try, the user can click on the "Next histogram" button.
The app will:
- generates parameters (mean, sd) and a new data set
- determine the min, max values and randomly generate a new "first guess" value
- update the slider based on the new min, max and "first guess"values
Histogram with guessed (red) and real mean (blue)
=======================
```{r, echo=FALSE}
genParams <- function()
{
# Generate random mean in between 25 and 75
mu = ceiling(runif(1, 25, 75))
# Generate random standard deviation in between 1 and 10
sd = ceiling(runif(1, 0, 10))
return(list(r_mu = mu, r_sd = sd))
}
genData <- function (aNumber, aMu, aSd)
{
# random generation for the normal distribution with mean equal to mean
# and standard deviation equal to sd
x <- rnorm(aNumber, mean = aMu, sd = aSd)
#if the range of the generated data is too samll generate again
while( 5 > diff(range(x)))
{
x <- rnorm(aNumber, mean = aMu, sd = aSd)
}
# The real mean and Sd for the generated dataset isn't exactly same as
# the parameters were. Calculate the real values
r_mu = mean(x)
r_sd = sd(x)
return(list(my_x = x, my_mu = r_mu, my_sd = r_sd))
}
# Generate new mean and sd
par <- genParams()
# Generate dataset wit 1000 elements using par
res <- genData(1000, par$r_mu, par$r_sd)
# Calculate boundary values to update slider
my_min <- round(min(res$my_x))
my_max <- round(max(res$my_x))
# generate random value for slider initial value
my_val <- ceiling(runif(1, my_min, my_max))
list_hist <- hist(res$my_x, xlab="Values", col='lightblue', main = 'Histogram')
# Calculate largest frequency value of the histogramm to draw
# user guest and real mean value lines
max_freq <- max(list_hist$counts)
mu <- res$my_mu * 0.95
lines(c(mu, mu), c(0, max_freq), col='red', lwd=5)
lines(c(res$my_mu, res$my_mu), c(0, max_freq), col='blue', lwd=5)
```