statsailr_procs_base 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab1d309ac74444b5b0cf7ae6d1097691dbf8c8437bb03853de68c727574ce396
4
- data.tar.gz: 245ddb37295e6c3dc5581aad3616ba39230bdf31b6aa0d5d33092e055e944432
3
+ metadata.gz: c71268a7ddcceaacdd0e8967bf3ce232667d43fe879e932b3acfc80a45235ecc
4
+ data.tar.gz: 822e0b2ab2bc4f040bd310e9fbe5acd883e2a4056e1c3c0a9abd70ac95945a07
5
5
  SHA512:
6
- metadata.gz: f2f4fa1fa6ba0b50bf3abf3bd220cce12efdffb2533dd0da3b35cba9d604728084fa913f02f1afcbfd6ed03b5cae67fe18bc5b8c2ed03b7428a436f471c172b6
7
- data.tar.gz: beba39a0885eff4d70243de4b78e601c30d26b6bdb29dbbb6436501564481b1f482a0beb10efbecf587f6d8350a934d130e592328ccdd6b3150a73a381faf315
6
+ metadata.gz: df438d9db6db33303a05ed4c3584f0cea3e336fc0915c2fa33da7484eb60d409b976cc97d87e02917e2babda8586dcfe5a148c2bfdfcee4094d1e55b8ba27dc7
7
+ data.tar.gz: 0731ce51dadc3ae5611ccbfcfd7a971065534efea91131656cc846eccfb9feb42488cee0c4f952106a435f1790122ac46f3d9124b80aaab80418c59a77025c6f
@@ -1,65 +1,68 @@
1
- sts_cat = new.env()
2
-
3
- sts_cat$table = function( data , vars , missing = FALSE, ... ){
4
- if( (! is.character(vars)) || length(vars) != 2 ){
5
- stop("vars argument requires character vector with size of 2")
6
- }
1
+ STS_RC_Cat = setRefClass("STS_RC_Cat")
2
+ STS_RC_Cat$methods(
3
+ table = function( data , vars , missing = FALSE, ... ){
4
+ if( (! is.character(vars)) || length(vars) != 2 ){
5
+ stop("vars argument requires character vector with size of 2")
6
+ }
7
7
 
8
- df_for_table = data[ , vars ]
9
- if( missing ){
10
- freq_ori = table(df_for_table, useNA = "ifany")
11
- }else{
12
- freq_ori = table(df_for_table)
13
- }
8
+ df_for_table = data[ , vars ]
9
+ if( missing ){
10
+ freq_ori = base::table(df_for_table, useNA = "ifany")
11
+ }else{
12
+ freq_ori = base::table(df_for_table)
13
+ }
14
14
 
15
- freq = addmargins( freq_ori )
16
- allper = format( addmargins( prop.table( freq_ori) * 100), digits = 4)
17
- rowper = format( addmargins( prop.table( freq_ori, 1) * 100), digits = 4)
18
- colper = format( addmargins( prop.table( freq_ori, 2) * 100), digits = 4)
15
+ freq = addmargins( freq_ori )
16
+ allper = format( addmargins( prop.table( freq_ori) * 100), digits = 4)
17
+ rowper = format( addmargins( prop.table( freq_ori, 1) * 100), digits = 4)
18
+ colper = format( addmargins( prop.table( freq_ori, 2) * 100), digits = 4)
19
19
 
20
- nrow = nrow(freq)
21
- ncol = ncol(freq)
22
- nrow_ori = nrow(freq_ori)
20
+ nrow = nrow(freq)
21
+ ncol = ncol(freq)
22
+ nrow_ori = nrow(freq_ori)
23
23
 
24
- temp = as.array( numeric( nrow * ncol * 4 ))
25
- dim( temp ) = c( nrow * 4, ncol )
26
- result = as.table( temp )
24
+ temp = as.array( numeric( nrow * ncol * 4 ))
25
+ dim( temp ) = c( nrow * 4, ncol )
26
+ result = base::as.table( temp )
27
27
 
28
- colnames(result) = c( colnames( freq_ori ) , "Total")
28
+ colnames(result) = c( colnames( freq_ori ) , "Total")
29
29
 
30
- row_label1 = c( rep("%",nrow_ori ) , "%")
31
- row_label2 = c( rep("Row%",nrow_ori ) , "")
32
- row_label3 = c( rep("Col%",nrow_ori ) , "")
30
+ row_label1 = c( rep("%",nrow_ori ) , "%")
31
+ row_label2 = c( rep("Row%",nrow_ori ) , "")
32
+ row_label3 = c( rep("Col%",nrow_ori ) , "")
33
33
 
34
- rownames(result) = as.vector( mapply( c, c( rownames( freq_ori ), "Total" ), row_label1 , row_label2 , row_label3 ))
34
+ rownames(result) = as.vector( mapply( c, c( rownames( freq_ori ), "Total" ), row_label1 , row_label2 , row_label3 ))
35
35
 
36
- cat( paste( "\t", names(freq), "\n", sep = "\t", collapse = "\t" ))
37
- for( irow in seq(1, nrow) ){
38
- for( icol in seq(1, ncol) ){
39
- result[ (irow-1)*4 + 1, icol] = freq[irow, icol]
40
- }
41
- for( icol in seq(1, ncol) ){
42
- result[ (irow-1)*4 + 2, icol] = allper[irow, icol]
43
- }
44
- for( icol in seq(1, ncol) ){
45
- if(irow != nrow && icol != ncol){
46
- result[ (irow-1)*4 + 3, icol] = rowper[irow, icol]
47
- }else{
48
- result[ (irow-1)*4 + 3, icol] = NA
36
+ cat( paste( "\t", names(freq), "\n", sep = "\t", collapse = "\t" ))
37
+ for( irow in seq(1, nrow) ){
38
+ for( icol in seq(1, ncol) ){
39
+ result[ (irow-1)*4 + 1, icol] = freq[irow, icol]
49
40
  }
50
- }
51
- for( icol in seq(1, ncol) ){
52
- if(icol != ncol && irow!= nrow){
53
- result[ (irow-1)*4 + 4, icol] = colper[irow, icol]
54
- }else{
55
- result[ (irow-1)*4 + 4, icol] = NA
41
+ for( icol in seq(1, ncol) ){
42
+ result[ (irow-1)*4 + 2, icol] = allper[irow, icol]
43
+ }
44
+ for( icol in seq(1, ncol) ){
45
+ if(irow != nrow && icol != ncol){
46
+ result[ (irow-1)*4 + 3, icol] = rowper[irow, icol]
47
+ }else{
48
+ result[ (irow-1)*4 + 3, icol] = NA
49
+ }
50
+ }
51
+ for( icol in seq(1, ncol) ){
52
+ if(icol != ncol && irow!= nrow){
53
+ result[ (irow-1)*4 + 4, icol] = colper[irow, icol]
54
+ }else{
55
+ result[ (irow-1)*4 + 4, icol] = NA
56
+ }
56
57
  }
57
58
  }
58
- }
59
59
 
60
- cat( paste( vars[1] , " vs ", vars[2], "\n" ) )
61
- print( result )
60
+ cat( paste( vars[1] , " vs ", vars[2], "\n" ) )
61
+ print( result )
62
+
63
+ return( freq_ori )
64
+ }
65
+ )
62
66
 
63
- return( freq_ori )
64
- }
67
+ sts_cat = STS_RC_Cat()
65
68
 
@@ -1,6 +1,9 @@
1
- sts_dev_copy = new.env()
1
+ STS_RC_DevCopy = setRefClass("STS_RC_DevCopy")
2
+ STS_RC_DevCopy$methods(
3
+ dev_copy = function( device, ... ){
4
+ dev.copy( device , ...)
5
+ dev.off()
6
+ }
7
+ )
2
8
 
3
- sts_dev_copy$dev_copy = function( device, ... ){
4
- dev.copy( device , ...)
5
- dev.off()
6
- }
9
+ sts_dev_copy = STS_RC_DevCopy()
@@ -1,7 +1,10 @@
1
- sts_factor = new.env()
1
+ STS_RC_Factor = setRefClass("STS_RC_Factor")
2
+ STS_RC_Factor$methods(
3
+ convert_to_factor = function( data, vars ){
4
+ data[vars] = lapply(data[vars], as.factor)
5
+ print( paste( "as.factor is applied to", paste( vars, collapse=",") , sep=" ") )
6
+ return( data )
7
+ }
8
+ )
2
9
 
3
- sts_factor$convert_to_factor = function( data, vars ){
4
- data[vars] = lapply(data[vars], as.factor)
5
- print( paste( "as.factor is applied to", paste( vars, collapse=",") , sep=" ") )
6
- return( data )
7
- }
10
+ sts_factor = STS_RC_Factor()
@@ -1,7 +1,10 @@
1
- sts_numeric = new.env()
1
+ STS_RC_Numeric = setRefClass("STS_RC_Numeric")
2
+ STS_RC_Numeric$methods(
3
+ convert_to_numeric = function( data, vars ){
4
+ data[vars] = lapply(data[vars], as.numeric)
5
+ print( paste( "as.numeric is applied to", paste( vars, collapse=",") , sep=" ") )
6
+ return( data )
7
+ }
8
+ )
2
9
 
3
- sts_numeric$convert_to_numeric = function( data, vars ){
4
- data[vars] = lapply(data[vars], as.numeric)
5
- print( paste( "as.numeric is applied to", paste( vars, collapse=",") , sep=" ") )
6
- return( data )
7
- }
10
+ sts_numeric = STS_RC_Numeric()
@@ -1,39 +1,42 @@
1
- sts_filter = new.env()
1
+ STS_RC_Filter = setRefClass("STS_RC_Filter")
2
+ STS_RC_Filter$methods(
3
+ wrap_filter = function( data, cond ){
4
+ lang_cond = rlang::parse_expr( cond )
5
+ df = dplyr::filter( data, !! lang_cond )
6
+ return(df)
7
+ },
2
8
 
3
- sts_filter$wrap_filter = function( data, cond ){
4
- lang_cond = rlang::parse_expr( cond )
5
- df = dplyr::filter( data, !! lang_cond )
6
- return(df)
7
- }
9
+ wrap_select = function( data, cond ){
10
+ lang_cond = rlang::parse_exprs(cond)
11
+ df = dplyr::select(data, !!! lang_cond)
12
+ return(df)
13
+ },
8
14
 
9
- sts_filter$wrap_select = function( data, cond ){
10
- lang_cond = rlang::parse_exprs(cond)
11
- df = dplyr::select(data, !!! lang_cond)
12
- return(df)
13
- }
14
-
15
- sts_filter$assign_to = function(var, df){
16
- if( (! is.character(var)) || length(var) != 1 ){
17
- stop("var argument requires character vector with size of 1")
18
- }
15
+ assign_to = function(var, df){
16
+ if( (! is.character(var)) || length(var) != 1 ){
17
+ stop("var argument requires character vector with size of 1")
18
+ }
19
19
 
20
- global_env = globalenv()
21
- global_env[[ var[1] ]] = df
22
- return(df)
23
- }
20
+ global_env = globalenv()
21
+ global_env[[ var[1] ]] = df
22
+ return(df)
23
+ },
24
24
 
25
- sts_filter$finalizer = function( df, last_inst, out ){
26
- if( last_inst != "assign_to" ){
27
- if( is.null(out) ){
28
- cat("The last result is print out, and is not assigned to any variable.\n")
29
- print(head(df))
30
- cat("Use out= option for PROC COMMAND or use assing_to instruction\n")
31
- cat("to assign the last result to some variable.\n")
25
+ finalizer = function( df, last_inst, out ){
26
+ if( last_inst != "assign_to" ){
27
+ if( is.null(out) ){
28
+ cat("The last result is print out, and is not assigned to any variable.\n")
29
+ print(head(df))
30
+ cat("Use out= option for PROC COMMAND or use assing_to instruction\n")
31
+ cat("to assign the last result to some variable.\n")
32
+ }else{
33
+ sts_filter$assign_to( out, df )
34
+ }
32
35
  }else{
33
- sts_filter$assign_to( out, df )
36
+ # OK. The result dataframe is already assigned using assign_to instruction.
34
37
  }
35
- }else{
36
- # OK. The result dataframe is already assigned using assign_to instruction.
37
38
  }
38
- }
39
+ )
40
+
41
+ sts_filter = STS_RC_Filter()
39
42
 
@@ -1,39 +1,42 @@
1
- sts_ggplot = new.env()
2
-
3
1
  library(rlang)
4
2
  library(ggplot2)
5
3
 
6
- sts_ggplot$mapping = function( data, assoc ){
7
- assoc_lang = rlang::parse_exprs(assoc)
8
- # https://github.com/tidyverse/ggplot2/issues/2675
9
- gg = ggplot2::ggplot(data, ggplot2::aes( ,, !!! assoc_lang ))
10
- return( gg )
11
- }
4
+ STS_RC_Ggplot = setRefClass("STS_RC_Ggplot")
5
+ STS_RC_Ggplot$methods(
6
+ mapping = function( data, assoc ){
7
+ assoc_lang = rlang::parse_exprs(assoc)
8
+ # https://github.com/tidyverse/ggplot2/issues/2675
9
+ gg = ggplot2::ggplot(data, ggplot2::aes( ,, !!! assoc_lang ))
10
+ return( gg )
11
+ },
12
12
 
13
- sts_ggplot$geom_point_wrapper = function( gg, params = NULL, ... ){
14
- if( is.null(params) ){
15
- gg = gg + ggplot2::geom_point()
16
- }else{
17
- params_lang = rlang::parse_exprs(params)
18
- # https://stackoverflow.com/questions/68379666/how-to-specify-a-package-or-namespace-for-rlangexec
19
- # https://stackoverflow.com/questions/70202220/big-bang-operator-for-ggplot2-geom-point-function
20
- # real_fun <- get("geom_point", envir=as.environment(paste0("package:", "ggplot2")))
21
- # gg = gg + rlang::exec(real_fun, !!! params_lang , ...)
22
- gg = gg + rlang::inject(ggplot2::geom_point( !!! params_lang , ...))
23
- }
24
- return( gg )
25
- }
13
+ geom_point_wrapper = function( gg, params = NULL, ... ){
14
+ if( is.null(params) ){
15
+ gg = gg + ggplot2::geom_point()
16
+ }else{
17
+ params_lang = rlang::parse_exprs(params)
18
+ # https://stackoverflow.com/questions/68379666/how-to-specify-a-package-or-namespace-for-rlangexec
19
+ # https://stackoverflow.com/questions/70202220/big-bang-operator-for-ggplot2-geom-point-function
20
+ # real_fun <- get("geom_point", envir=as.environment(paste0("package:", "ggplot2")))
21
+ # gg = gg + rlang::exec(real_fun, !!! params_lang , ...)
22
+ gg = gg + rlang::inject(ggplot2::geom_point( !!! params_lang , ...))
23
+ }
24
+ return( gg )
25
+ },
26
+
27
+ geom_histogram_wrapper = function( gg, params = NULL, ...){
28
+ if( is.null(params) ){
29
+ gg = gg + ggplot2::geom_histogram()
30
+ }else{
31
+ params_lang = rlang::parse_exprs(params)
32
+ gg = gg + rlang::inject(ggplot2::geom_histogram( !!! params_lang , ...))
33
+ }
34
+ return( gg )
35
+ },
26
36
 
27
- sts_ggplot$geom_histogram_wrapper = function( gg, params = NULL, ...){
28
- if( is.null(params) ){
29
- gg = gg + ggplot2::geom_histogram()
30
- }else{
31
- params_lang = rlang::parse_exprs(params)
32
- gg = gg + rlang::inject(ggplot2::geom_histogram( !!! params_lang , ...))
37
+ finalizer = function( gg ){
38
+ plot(gg)
33
39
  }
34
- return( gg )
35
- }
40
+ )
36
41
 
37
- sts_ggplot$finalizer = function( gg ){
38
- plot(gg)
39
- }
42
+ sts_ggplot = STS_RC_Ggplot()
@@ -1,5 +1,6 @@
1
1
  module ProcGgplot
2
2
  include ProcSettingModule
3
+ add_setting_from( __dir__, "proc_common/factor.rb" )
3
4
 
4
5
  source_r_file( __dir__, File.basename(__FILE__ , ".rb") + ".R")
5
6
  validate_option("data", is_a: ["SymbolR", "String"], as: "SymbolR" , required: true)
@@ -1,55 +1,54 @@
1
- sts_group = new.env()
2
-
3
1
  library(rlang)
4
2
 
5
- sts_group$wrap_group_by = function(data, vars){
3
+ STS_RC_Group = setRefClass("STS_RC_Group")
4
+ STS_RC_Group$methods(
5
+ wrap_group_by = function(data, vars){
6
6
  vars_lang = rlang::parse_exprs(vars)
7
7
  grouped_data = rlang::inject(dplyr::group_by( data, !!! vars_lang ))
8
8
  return(grouped_data)
9
- }
10
-
9
+ },
11
10
 
12
- sts_group$wrap_mutate = function(data, params){
11
+ wrap_mutate = function(data, params){
13
12
  params_lang = rlang::parse_exprs(params)
14
13
  mutated_data = rlang::inject(dplyr::mutate( data, !!! params_lang ))
15
14
  return(mutated_data)
16
- }
15
+ },
17
16
 
18
-
19
- sts_group$wrap_summarize = function(data, params){
17
+ wrap_summarize = function(data, params){
20
18
  params_lang = rlang::parse_exprs(params)
21
19
  result = rlang::inject(dplyr::summarize( data, !!! params_lang ))
22
20
  print(result)
23
21
  return(data)
24
- }
25
-
22
+ },
26
23
 
27
- sts_group$assign_to = function(var, df){
28
- df = as.data.frame(df)
24
+ assign_to = function(var, df){
25
+ df = as.data.frame(df)
29
26
 
30
- if( (! is.character(var)) || length(var) != 1 ){
31
- stop("var argument requires character vector with size of 1")
32
- }
33
-
34
- global_env = globalenv()
35
- global_env[[ var[1] ]] = df
36
- return(df)
37
- }
38
-
39
- sts_group$finalizer = function( df, last_inst, out ){
40
- df = as.data.frame(df)
27
+ if( (! is.character(var)) || length(var) != 1 ){
28
+ stop("var argument requires character vector with size of 1")
29
+ }
41
30
 
42
- if( last_inst != "assign_to" ){
43
- if( is.null(out) ){
44
- cat("The last result is print out, and is not assigned to any variable.\n")
45
- print(head(df))
46
- cat("Use out= option for PROC COMMAND or use assing_to instruction\n")
47
- cat("to assign the last result to some variable.\n")
31
+ global_env = globalenv()
32
+ global_env[[ var[1] ]] = df
33
+ return(df)
34
+ },
35
+
36
+ finalizer = function( df, last_inst, out ){
37
+ df = as.data.frame(df)
38
+
39
+ if( last_inst != "assign_to" ){
40
+ if( is.null(out) ){
41
+ cat("The last result is print out, and is not assigned to any variable.\n")
42
+ print(head(df))
43
+ cat("Use out= option for PROC COMMAND or use assing_to instruction\n")
44
+ cat("to assign the last result to some variable.\n")
45
+ }else{
46
+ sts_group$assign_to( out, df )
47
+ }
48
48
  }else{
49
- sts_group$assign_to( out, df )
49
+ # OK. The result dataframe is already assigned using assign_to instruction.
50
50
  }
51
- }else{
52
- # OK. The result dataframe is already assigned using assign_to instruction.
53
51
  }
54
- }
52
+ )
55
53
 
54
+ sts_group = STS_RC_Group()
@@ -1,12 +1,13 @@
1
- sts_mult = new.env()
2
-
3
- sts_mult$p_adjust = function( x , method ){
4
- ori_p = summary(x)[[1]][['Pr(>F)']]
5
- result = p.adjust( ori_p, method )
6
-
7
- cat( "p adjustment\n" )
8
- cat( paste( "Method:", method, "\n", sep=" ", collapse=" " ) )
9
- return(result)
10
- }
11
-
12
-
1
+ STS_RC_Mult = setRefClass("STS_RC_Mult")
2
+ STS_RC_Mult$methods(
3
+ p_adjust = function( x , method ){
4
+ ori_p = summary(x)[[1]][['Pr(>F)']]
5
+ result = p.adjust( ori_p, method )
6
+
7
+ cat( "p adjustment\n" )
8
+ cat( paste( "Method:", method, "\n", sep=" ", collapse=" " ) )
9
+ return(result)
10
+ }
11
+ )
12
+
13
+ sts_mult = STS_RC_Mult()
@@ -1,52 +1,55 @@
1
- sts_plot = new.env()
2
-
3
- sts_plot$legend = function( legend , ... ){
4
- if( (! is.character(legend)) ){
5
- stop("legend argument requires character vector")
6
- }
7
-
8
- args = list(...)
9
- args$legend = legend
10
- args$x = if_exist_else ( "x", args, paste( "topleft" ))
11
-
12
- do.call( get_pkg_fun( "graphics::legend") , args )
13
- }
14
-
15
- sts_plot$hist = function( data , var , ... ){
16
- if( (! is.character(var)) || length(var) != 1 ){
17
- stop("vars argument requires character vector with size of 1")
18
- }
19
-
20
- args = list(...)
21
- args$x = data[[var]]
22
- args$main = if_exist_else ( "main", args, paste( "Frequency of" , var ))
23
- args$xlab = if_exist_else ( "xlab", args, paste( var ))
24
-
25
- do.call( get_pkg_fun( "graphics::hist") , args )
26
- }
27
-
28
- sts_plot$box = function( data , var , ... ){
29
- if( (! is.character(var)) || length(var) != 1 ){
30
- stop("vars argument requires character vector with size of 1")
1
+ STS_RC_Plot = setRefClass("STS_RC_Plot")
2
+ STS_RC_Plot$methods(
3
+ legend = function( legend , ... ){
4
+ if( (! is.character(legend)) ){
5
+ stop("legend argument requires character vector")
6
+ }
7
+
8
+ args = list(...)
9
+ args$legend = legend
10
+ args$x = if_exist_else ( "x", args, paste( "topleft" ))
11
+
12
+ do.call( get_pkg_fun( "graphics::legend") , args )
13
+ },
14
+
15
+ hist = function( data , var , ... ){
16
+ if( (! is.character(var)) || length(var) != 1 ){
17
+ stop("vars argument requires character vector with size of 1")
18
+ }
19
+
20
+ args = list(...)
21
+ args$x = data[[var]]
22
+ args$main = if_exist_else ( "main", args, paste( "Frequency of" , var ))
23
+ args$xlab = if_exist_else ( "xlab", args, paste( var ))
24
+
25
+ do.call( get_pkg_fun( "graphics::hist") , args )
26
+ },
27
+
28
+ box = function( data , var , ... ){
29
+ if( (! is.character(var)) || length(var) != 1 ){
30
+ stop("vars argument requires character vector with size of 1")
31
+ }
32
+
33
+ args = list(...)
34
+ args$x = data[[var]]
35
+ args$main = if_exist_else ( "main", args, paste( var ))
36
+ args$lex.order = TRUE
37
+
38
+ do.call( get_pkg_fun( "graphics::boxplot"), args )
39
+ },
40
+
41
+ scatter = function( data , vars , ... ){
42
+ if( (! is.character(vars)) || length(vars) != 2 ){
43
+ stop("vars argument requires character vector with size of 2")
44
+ }
45
+
46
+ args = list(...)
47
+ args$x = data[vars]
48
+ args$main = if_exist_else ( "main", args, paste( vars[1], "vs", vars[2] ))
49
+
50
+ do.call( get_pkg_fun( "graphics::plot"), args )
31
51
  }
52
+ )
32
53
 
33
- args = list(...)
34
- args$x = data[[var]]
35
- args$main = if_exist_else ( "main", args, paste( var ))
36
- args$lex.order = TRUE
37
-
38
- do.call( get_pkg_fun( "graphics::boxplot"), args )
39
- }
40
-
41
- sts_plot$scatter = function( data , vars , ... ){
42
- if( (! is.character(vars)) || length(vars) != 2 ){
43
- stop("vars argument requires character vector with size of 2")
44
- }
45
-
46
- args = list(...)
47
- args$x = data[vars]
48
- args$main = if_exist_else ( "main", args, paste( vars[1], "vs", vars[2] ))
49
-
50
- do.call( get_pkg_fun( "graphics::plot"), args )
51
- }
54
+ sts_plot = STS_RC_Plot()
52
55
 
@@ -1,60 +1,62 @@
1
- sts_print = new.env()
2
-
3
- sts_print$nth = function( x , positions ){
4
-
5
- # positions are passed as strvec which is already splitted by spaces and signs
6
- # group these elements
7
-
8
- shift_vec = function( x, n , fill=NA ){
9
- if( n == 0 ){
10
- return( x )
11
- }else if( n > 0 ){
12
- return( c( rep(fill, n), head(x, -n)) )
13
- }else if( n < 0 ){
14
- return( c( tail(x, length(x) + n), rep(fill, -n)) )
15
- }else{
16
- print("inappropriate n.")
17
- return(x)
18
- }
19
- }
20
-
21
- colon_pos = (positions == ":")
22
- colon_after_pos = shift_vec( colon_pos , 1, F )
23
- group_num = cumsum( !( colon_pos | colon_after_pos) )
24
-
25
- position_strvec = sapply( split(positions, group_num) , function(elem){ paste( elem, collapse="" )} )
26
- names( position_strvec) = position_strvecp
27
-
28
- # Convert them to a list of int vectors
29
-
30
- position_list = lapply(position_strvec, function(elem){
31
- range_sep = ":"
32
- if( grepl( range_sep, elem, fixed = TRUE) ){ # grepl(pattern, x)
33
- range = strsplit(elem, range_sep, fixed=TRUE)[[1]]
34
- if( length(range) != 2){
35
- print("Range should be specified with x:y form (meaning from x to y).")
36
- return( 0 )
37
- }else{
38
- range_int = strtoi(range)
39
- return( seq(range_int[1], range_int[2]) )
1
+ STS_RC_Print = setRefClass("STS_RC_Print")
2
+ STS_RC_Print$methods(
3
+ nth = function( x , positions ){
4
+
5
+ # positions are passed as strvec which is already splitted by spaces and signs
6
+ # group these elements
7
+
8
+ shift_vec = function( x, n , fill=NA ){
9
+ if( n == 0 ){
10
+ return( x )
11
+ }else if( n > 0 ){
12
+ return( c( rep(fill, n), head(x, -n)) )
13
+ }else if( n < 0 ){
14
+ return( c( tail(x, length(x) + n), rep(fill, -n)) )
15
+ }else{
16
+ print("inappropriate n.")
17
+ return(x)
18
+ }
40
19
  }
41
- }else{
42
- return( strtoi(elem))
43
- }
44
- })
45
-
46
- # For each int vector, print corresponding rows
47
20
 
48
- lapply( position_list, function(nth){
49
- x[ nth , ]
50
- })
51
-
52
- }
21
+ colon_pos = (positions == ":")
22
+ colon_after_pos = shift_vec( colon_pos , 1, F )
23
+ group_num = cumsum( !( colon_pos | colon_after_pos) )
24
+
25
+ position_strvec = sapply( split(positions, group_num) , function(elem){ paste( elem, collapse="" )} )
26
+
27
+ # Convert them to a list of int vectors
28
+
29
+ position_list = lapply(position_strvec, function(elem){
30
+ range_sep = ":"
31
+ if( grepl( range_sep, elem, fixed = TRUE) ){ # grepl(pattern, x)
32
+ range = strsplit(elem, range_sep, fixed=TRUE)[[1]]
33
+ if( length(range) != 2){
34
+ print("Range should be specified with x:y form (meaning from x to y).")
35
+ return( 0 )
36
+ }else{
37
+ range_int = strtoi(range)
38
+ return( seq(range_int[1], range_int[2]) )
39
+ }
40
+ }else{
41
+ return( strtoi(elem))
42
+ }
43
+ })
44
+
45
+ # For each int vector, print corresponding rows
46
+
47
+ lapply( position_list, function(nth){
48
+ x[ nth , ]
49
+ })
50
+
51
+ },
52
+
53
+ random = function( x , n ){
54
+ row_num = nrow(x)
55
+ index = ceiling(row_num * runif(n))
56
+ sorted_index = sort(index)
57
+ x[ sorted_index , ]
58
+ }
59
+ )
53
60
 
54
- sts_print$random = function( x , n ){
55
- row_num = nrow(x)
56
- index = ceiling(row_num * runif(n))
57
- sorted_index = sort(index)
58
- x[ sorted_index , ]
59
- }
61
+ sts_print = STS_RC_Print()
60
62
 
@@ -1,32 +1,35 @@
1
- sts_sort = new.env()
1
+ STS_RC_Sort = setRefClass("STS_RC_Sort")
2
+ STS_RC_Sort$methods(
3
+ wrap_arrange = function( data, cond ){
4
+ lang_cond = rlang::parse_exprs(cond)
5
+ df = dplyr::arrange(data, !!! lang_cond)
6
+ return(df)
7
+ },
2
8
 
3
- sts_sort$wrap_arrange = function( data, cond ){
4
- lang_cond = rlang::parse_exprs(cond)
5
- df = dplyr::arrange(data, !!! lang_cond)
6
- return(df)
7
- }
8
-
9
- sts_sort$assign_to = function(var, df){
10
- if( (! is.character(var)) || length(var) != 1 ){
11
- stop("var argument requires character vector with size of 1")
12
- }
9
+ assign_to = function(var, df){
10
+ if( (! is.character(var)) || length(var) != 1 ){
11
+ stop("var argument requires character vector with size of 1")
12
+ }
13
13
 
14
- global_env = globalenv()
15
- global_env[[ var[1] ]] = df
16
- return(df)
17
- }
14
+ global_env = globalenv()
15
+ global_env[[ var[1] ]] = df
16
+ return(df)
17
+ },
18
18
 
19
- sts_sort$finalizer = function( df, last_inst, out ){
20
- if( last_inst != "assign_to" ){
21
- if( is.null(out) ){
22
- cat("The last result is print out, and is not assigned to any variable.\n")
23
- print(head(df))
24
- cat("Use out= option for PROC COMMAND or use assing_to instruction\n")
25
- cat("to assign the last result to some variable.\n")
19
+ finalizer = function( df, last_inst, out ){
20
+ if( last_inst != "assign_to" ){
21
+ if( is.null(out) ){
22
+ cat("The last result is print out, and is not assigned to any variable.\n")
23
+ print(head(df))
24
+ cat("Use out= option for PROC COMMAND or use assing_to instruction\n")
25
+ cat("to assign the last result to some variable.\n")
26
+ }else{
27
+ sts_sort$assign_to( out, df )
28
+ }
26
29
  }else{
27
- sts_sort$assign_to( out, df )
30
+ # OK. The result dataframe is already assigned using assign_to instruction.
28
31
  }
29
- }else{
30
- # OK. The result dataframe is already assigned using assign_to instruction.
31
32
  }
32
- }
33
+ )
34
+
35
+ sts_sort = STS_RC_Sort()
@@ -1,33 +1,35 @@
1
- sts_two = new.env()
1
+ STS_RC_Two = setRefClass("STS_RC_Two")
2
+ STS_RC_Two$methods(
3
+ t_test = function( data , vars , ... ){
4
+ if( (! is.character(vars)) || length(vars) != 2 ){
5
+ stop("vars argument requires character vector with size of 2")
6
+ }
7
+ x = vars[1]
8
+ y = vars[2]
9
+ result = t.test( data[[x]], data[[y]], ... )
10
+ return(result)
11
+ },
2
12
 
3
- sts_two$t_test = function( data , vars , ... ){
4
- if( (! is.character(vars)) || length(vars) != 2 ){
5
- stop("vars argument requires character vector with size of 2")
6
- }
7
- x = vars[1]
8
- y = vars[2]
9
- result = t.test( data[[x]], data[[y]], ... )
10
- return(result)
11
- }
12
-
13
- sts_two$paired = function( data , vars , ... ){
14
- if( (! is.character(vars)) || length(vars) != 2 ){
15
- stop("vars argument requires character vector with size of 2")
16
- }
17
- x = vars[1]
18
- y = vars[2]
19
- result = t.test( data[[x]], data[[y]], paired = TRUE, ... )
20
- return(result)
21
- }
13
+ paired = function( data , vars , ... ){
14
+ if( (! is.character(vars)) || length(vars) != 2 ){
15
+ stop("vars argument requires character vector with size of 2")
16
+ }
17
+ x = vars[1]
18
+ y = vars[2]
19
+ result = t.test( data[[x]], data[[y]], paired = TRUE, ... )
20
+ return(result)
21
+ },
22
22
 
23
- sts_two$wilcox_test = function( data , vars , ... ){
24
- if( (! is.character(vars)) || length(vars) != 2 ){
25
- stop("vars argument requires character vector with size of 2")
23
+ wilcox_test = function( data , vars , ... ){
24
+ if( (! is.character(vars)) || length(vars) != 2 ){
25
+ stop("vars argument requires character vector with size of 2")
26
+ }
27
+ x = vars[1]
28
+ y = vars[2]
29
+ result = wilcox.test( data[[x]], data[[y]], ... )
30
+ return(result)
26
31
  }
27
- x = vars[1]
28
- y = vars[2]
29
- result = wilcox.test( data[[x]], data[[y]], ... )
30
- return(result)
31
- }
32
+ )
32
33
 
34
+ sts_two = STS_RC_Two()
33
35
 
@@ -1,126 +1,125 @@
1
- sts_uni = new.env()
2
-
3
- sts_uni$list2str = function ( lst, exclude=c() ){
4
- str = ""
5
- for( i in seq(1,length(lst))){
6
- if (! names(lst)[i] %in% exclude ){
7
- key = names(lst)[i]
8
- value = lst[[i]]
9
-
10
- if( length( value ) == 1 ){
11
- str = paste0( str, key, "\t", value, "\n")
12
- }else if( is.null( attr( value, "names"))){
13
- str = paste0( str, key, "\t", paste( value , collapse=" ") , "\n")
14
- }else{
15
- str = paste0( str, key, "\n" )
16
- str = paste0( str, paste( mapply( paste0, "\t", names(value), "\t" , value), collapse="\n") , "\n")
1
+ STS_RC_Uni = setRefClass("STS_RC_Uni")
2
+ STS_RC_Uni$methods(
3
+ list2str = function ( lst, exclude=c() ){
4
+ str = ""
5
+ for( i in seq(1,length(lst))){
6
+ if (! names(lst)[i] %in% exclude ){
7
+ key = names(lst)[i]
8
+ value = lst[[i]]
9
+
10
+ if( length( value ) == 1 ){
11
+ str = paste0( str, key, "\t", value, "\n")
12
+ }else if( is.null( attr( value, "names"))){
13
+ str = paste0( str, key, "\t", paste( value , collapse=" ") , "\n")
14
+ }else{
15
+ str = paste0( str, key, "\n" )
16
+ str = paste0( str, paste( mapply( paste0, "\t", names(value), "\t" , value), collapse="\n") , "\n")
17
+ }
17
18
  }
18
19
  }
19
- }
20
- return( str )
21
- }
22
-
23
-
24
- sts_uni$var = function( data , vars , hist = FALSE , ... ){
25
- if( (! is.character(vars)) || length(vars) == 0 ){
26
- stop("vars argument requires character vector")
27
- }
28
- check_name_existance = vars %in% colnames(data)
29
- if( ! all(check_name_existance) ){
30
- print( paste( check_name_existance , "\n") )
31
- stop("vars argument should be colnames of data")
32
- }
20
+ return( str )
21
+ },
33
22
 
34
- results = list()
35
- df = data
23
+ var = function( data , vars , hist = FALSE , ... ){
24
+ if( (! is.character(vars)) || length(vars) == 0 ){
25
+ stop("vars argument requires character vector")
26
+ }
27
+ check_name_existance = vars %in% colnames(data)
28
+ if( ! all(check_name_existance) ){
29
+ print( paste( check_name_existance , "\n") )
30
+ stop("vars argument should be colnames of data")
31
+ }
36
32
 
37
- for( var in vars){
38
- result = list()
39
- result$vec = df[[ var ]]
33
+ results = list()
34
+ df = data
40
35
 
41
- result$mean = mean( df[[ var ]] )
42
- result$N = length( df[[ var ]] )
43
- missing_pos = is.na(df[[ var ]])
36
+ for( var in vars){
37
+ result = list()
38
+ result$vec = df[[ var ]]
44
39
 
45
- result$missing = sum( missing_pos )
46
- result$n = sum( ! missing_pos )
40
+ result$mean = mean( df[[ var ]] )
41
+ result$N = length( df[[ var ]] )
42
+ missing_pos = is.na(df[[ var ]])
47
43
 
48
- non_missing = df[[ var ]][! missing_pos]
44
+ result$missing = sum( missing_pos )
45
+ result$n = sum( ! missing_pos )
49
46
 
50
- quantiles = quantile(non_missing, probs = c(1, 0.99, 0.95, 0.90, 0.75, 0.5, 0.25, 0.1, 0.05, 0.01, 0), na.rm = TRUE)
51
- third_quart = quantiles[5]
52
- first_quart = quantiles[7]
53
- result$max = head(quantiles, 1)
54
- result$min = tail(quantiles, 1)
47
+ non_missing = df[[ var ]][! missing_pos]
55
48
 
56
- result$mean = mean(non_missing)
57
- result$deviation = sd(non_missing)
58
- result$median = median(non_missing)
59
- result$IQR = c( first_quart ,third_quart )
60
- result$quantiles = quantiles
49
+ quantiles = quantile(non_missing, probs = c(1, 0.99, 0.95, 0.90, 0.75, 0.5, 0.25, 0.1, 0.05, 0.01, 0), na.rm = TRUE)
50
+ third_quart = quantiles[5]
51
+ first_quart = quantiles[7]
52
+ result$max = head(quantiles, 1)
53
+ result$min = tail(quantiles, 1)
61
54
 
62
- # To be implemented
63
- # skewness
64
- # kurtosis
55
+ result$mean = mean(non_missing)
56
+ result$deviation = sd(non_missing)
57
+ result$median = median(non_missing)
58
+ result$IQR = c( first_quart ,third_quart )
59
+ result$quantiles = quantiles
65
60
 
66
- results[[var]] = result
67
- }
61
+ # To be implemented
62
+ # skewness
63
+ # kurtosis
68
64
 
69
- result_str = ""
70
- result_num = length(results)
71
- for( i in seq(1, result_num)){
72
- result_str = paste0( result_str, names(results)[i], " ", "statistics\n")
73
- result_str = paste0( result_str, sts_uni$list2str(results[[i]], exclude=c("vec")), "\n")
74
- }
65
+ results[[var]] = result
66
+ }
75
67
 
76
- # output
77
- if( hist ){
78
- layout( matrix(seq(1, result_num), ncol = 1))
79
- for( i in seq(1,result_num)){
80
- hist( results[[ i ]]$vec , main = paste( "Histogram of" , names(results)[i] ), xlab=names(results)[i] )
68
+ result_str = ""
69
+ result_num = length(results)
70
+ for( i in seq(1, result_num)){
71
+ result_str = paste0( result_str, names(results)[i], " ", "statistics\n")
72
+ result_str = paste0( result_str, sts_uni$list2str(results[[i]], exclude=c("vec")), "\n")
81
73
  }
82
- layout( matrix(c(1), ncol = 1))
83
- }
84
74
 
85
- cat( result_str ) # output is done using cat()
86
- return(results) # return value to be used by other instructions is results
87
- }
75
+ # output
76
+ if( hist ){
77
+ layout( matrix(seq(1, result_num), ncol = 1))
78
+ for( i in seq(1,result_num)){
79
+ hist( results[[ i ]]$vec , main = paste( "Histogram of" , names(results)[i] ), xlab=names(results)[i] )
80
+ }
81
+ layout( matrix(c(1), ncol = 1))
82
+ }
88
83
 
84
+ cat( result_str ) # output is done using cat()
85
+ return(results) # return value to be used by other instructions is results
86
+ },
89
87
 
90
- sts_uni$qqplot = function( results , var = NULL , qqline = FALSE, ... ){
91
- if ( (! is.null(var)) && (length(var) != 1) ){
92
- stop("main argument needs to be length of 1 character vector")
93
- }
94
- if( is.null(var) ){
95
- vec = results[[1]]$vec
96
- }else{
97
- vec = results[[var]]$vec
98
- }
99
- qqnorm(vec)
100
- if( qqline ){
101
- qqline(vec)
102
- }
103
- }
88
+ qqplot = function( results , var = NULL , qqline = FALSE, ... ){
89
+ if ( (! is.null(var)) && (length(var) != 1) ){
90
+ stop("main argument needs to be length of 1 character vector")
91
+ }
92
+ if( is.null(var) ){
93
+ vec = results[[1]]$vec
94
+ }else{
95
+ vec = results[[var]]$vec
96
+ }
97
+ qqnorm(vec)
98
+ if( qqline ){
99
+ qqline(vec)
100
+ }
101
+ },
104
102
 
105
- sts_uni$hist = function( data, vars ){
106
- if ( (! is.null(var)) && (length(var) != 1) ){
107
- stop("main argument needs to be length of 1 character vector")
108
- }
103
+ hist = function( data, vars ){
104
+ if ( (! is.null(var)) && (length(var) != 1) ){
105
+ stop("main argument needs to be length of 1 character vector")
106
+ }
109
107
 
110
- size = length(vars)
111
- par(mfrow(size, 1))
108
+ size = length(vars)
109
+ par(mfrow(size, 1))
112
110
 
113
- for( i in seq(1, size)){
114
- var = vars[[i]]
115
- g = data[[var]]
116
- h <- hist(g, breaks = 10, density = 10,
111
+ for( i in seq(1, size)){
112
+ var = vars[[i]]
113
+ g = data[[var]]
114
+ h <- hist(g, breaks = 10, density = 10,
117
115
  col = "lightgray", xlab = "Accuracy", main = "Overall")
118
- xfit <- seq(min(g), max(g), length = 40)
119
- yfit <- dnorm(xfit, mean = mean(g), sd = sd(g))
120
- yfit <- yfit * diff(h$mids[1:2]) * length(g)
116
+ xfit <- seq(min(g), max(g), length = 40)
117
+ yfit <- dnorm(xfit, mean = mean(g), sd = sd(g))
118
+ yfit <- yfit * diff(h$mids[1:2]) * length(g)
121
119
 
122
- lines(xfit, yfit, col = "black", lwd = 2)
120
+ lines(xfit, yfit, col = "black", lwd = 2)
121
+ }
123
122
  }
123
+ )
124
124
 
125
- }
126
-
125
+ sts_uni = STS_RC_Uni()
@@ -22,6 +22,7 @@ module ProcUni
22
22
  setting.runtime_args = {"results" => result("var")}
23
23
  setting.store_result = false
24
24
  setting.print_opt = false
25
+ setting.plot_opt = true
25
26
  end
26
27
  end
27
28
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module StatSailr
4
4
  module ProcsBase
5
- VERSION = "0.1.2"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statsailr_procs_base
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshihiro Umehara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-06 00:00:00.000000000 Z
11
+ date: 2021-12-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This 'statsailr_procs_base' gem provides a collection of fundamental
14
14
  PROCs for StatSailr program. This gem is essential for StatSailr to provide a useful