1
1
library(shiny )
2
2
3
+ rd = 1 # digits to round
4
+
5
+ set.seed(19 )
6
+ n = 20
7
+ sigma = 5
8
+
9
+ x <- rnorm(n , 2 , 1 )
10
+ a_true = 10
11
+ b_true = 3
12
+ y <- a_true + b_true * x + rnorm(n , 0 , sigma )
13
+
3
14
ui <- fluidPage(
4
15
br(),
5
16
br(),
6
- sidebarPanel(sliderInput(" scale_x" , " Rescale X" , min = - 5 ,
7
- max = 5 , step = .5 , value = 1 ),
8
- sliderInput(" scale_y" , " Rescale Y" , min = - 5 ,
9
- max = 5 , step = .5 , value = 1 ),
17
+ sidebarPanel(sliderInput(" scale_x" , " Rescale X" , min = - 2 ,
18
+ max = 2 , step = .5 , value = 1 ),
19
+ sliderInput(" scale_y" , " Rescale Y" , min = - 0. 5 ,
20
+ max = 2 , step = .5 , value = 1 ),
10
21
br(),
11
22
br(),
12
23
@@ -21,20 +32,11 @@ ui <- fluidPage(
21
32
server <- function (input ,output ){
22
33
output $ best_fit <- renderText({
23
34
24
- rd = 1 # digits to round
25
-
26
- set.seed(19 )
27
- n = 10000
28
- sigma = 5
29
-
30
- x <- rnorm(n , 2 , 40 )
31
- y <- 10 + 3 * x + rnorm(n , 0 , sigma )
32
-
33
35
s_x <- input $ scale_x
34
36
s_y <- input $ scale_y
35
37
36
- orig_slope <- (cov( x , y ) / var( x ))
37
- orig_inter <- mean( y ) - orig_slope * mean( x )
38
+ orig_slope <- b_true
39
+ orig_inter <- a_true
38
40
39
41
best_slope <- (s_y / s_x ) * orig_slope # scale
40
42
best_inter <- s_y * orig_inter
@@ -55,23 +57,15 @@ server <- function(input,output){
55
57
56
58
output $ regPlot_rescale <- renderPlot({
57
59
58
- set.seed(19 )
59
- n = 100
60
- sigma = 5
61
-
62
- x <- rnorm(n , 2 , 10 )
63
- y <- 10 + 3 * x + rnorm(n , 0 , sigma )
64
-
65
60
s_x <- input $ scale_x
66
61
s_y <- input $ scale_y
67
62
68
63
fit <- lm(y ~ x , data.frame (x = (s_x * x ), y = (s_y * y )))
69
64
70
-
71
65
plot((s_x * x ), (s_y * y ), type = " p" , pch = 21 , col = " blue" , bg = " royalblue" ,
72
- xlim = c(- 50 , 50 ),
66
+ xlim = c(- 5 , 10 ),
73
67
xlab = paste0(s_x , " X" ),
74
- ylim = c(- 200 , 300 ),
68
+ ylim = c(- 10 , 45 ),
75
69
ylab = paste0(s_y , " Y" ),
76
70
main = " Rescale X and Y" , frame.plot = FALSE ,
77
71
cex = 1 . )
@@ -82,8 +76,7 @@ server <- function(input,output){
82
76
})
83
77
84
78
output $ DGP <- renderText({
85
- n = 100
86
- paste0(" Data Generating Process: Y = 10 + 3X + error\n Sample Size N = " ,n )
79
+ paste0(" Data Generating Process: Y = 10 + 3X + error" )
87
80
})
88
81
89
82
}
0 commit comments