@@ -33,18 +33,18 @@ d_ex <- wrapr::build_frame( |
33 | 33 |
|
34 | 34 | ``` r |
35 | 35 | build_traj <- function(d_ex, lambda_max = 4, length.out = 100) { |
36 | | - frames <- list() |
| 36 | + frame <- list() |
37 | 37 | suppressWarnings( |
38 | 38 | for(lambda in seq(0, lambda_max, length.out = length.out)) { |
39 | 39 | model <- glm(y ~ x1 + x2, family=binomial(), data=d_ex, weights = 1 + lambda * d_ex$y) |
40 | 40 | preds <- predict(model, type='response', newdata=d_ex) |
41 | 41 | row_frame <- as.data.frame(t(as.matrix(preds))) |
42 | 42 | row_frame['lambda'] <- lambda |
43 | | - frames <- c(frames, list(row_frame)) |
| 43 | + frame <- c(frame, list(row_frame)) |
44 | 44 | } |
45 | 45 | ) |
46 | | - frames <- do.call(rbind, frames) |
47 | | - return(frames) |
| 46 | + frame <- do.call(rbind, frame) |
| 47 | + return(frame) |
48 | 48 | } |
49 | 49 | ``` |
50 | 50 |
|
@@ -53,39 +53,63 @@ logit <- function(x) {log(x/(1-x))} |
53 | 53 | ``` |
54 | 54 |
|
55 | 55 | ``` r |
56 | | -frames <- build_traj(d_ex) |
| 56 | +frame <- build_traj(d_ex) |
57 | 57 | ``` |
58 | 58 |
|
59 | 59 | ``` r |
60 | | -mk_example <- function() { |
61 | | - d_cols <- c('x1', 'x2', 'x3', 'y') |
62 | | - n_rows <- 8 |
63 | | - while(TRUE) { |
64 | | - d_ex <- data.frame(matrix( |
65 | | - sample(seq(-5, 5), size = length(d_cols) * n_rows, replace = TRUE), |
66 | | - ncol=length(d_cols))) |
67 | | - colnames(d_ex) <- d_cols |
68 | | - d_ex['y'] = d_ex['y'] >= 0 |
69 | | - frames <- build_traj(d_ex) |
70 | | - n_f_rows <- nrow(frames) |
| 60 | +want_frame <- function(frame) { |
| 61 | + n_f_rows <- nrow(frame) |
71 | 62 | have_cross <- FALSE |
72 | 63 | have_increasing <- FALSE |
73 | 64 | have_decreasing <- FALSE |
74 | | - for(col in colnames(frames)) { |
| 65 | + for(col in colnames(frame)) { |
75 | 66 | if(col != 'lambda') { |
76 | | - col_v <- frames[[col]] |
| 67 | + col_v <- frame[[col]] |
| 68 | + if( (min(col_v) + 1e2 < min(col_v[[1]], col_v[[n_f_rows]])) || (max(col_v) - 1e2 > max(col_v[[1]], col_v[[n_f_rows]])) ) { |
| 69 | + return(TRUE) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return(FALSE) |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +``` r |
| 78 | +want_frame <- function(frame) { |
| 79 | + n_f_rows <- nrow(frame) |
| 80 | + have_cross <- FALSE |
| 81 | + have_increasing <- FALSE |
| 82 | + have_decreasing <- FALSE |
| 83 | + for(col in colnames(frame)) { |
| 84 | + if(col != 'lambda') { |
| 85 | + col_v <- frame[[col]] |
77 | 86 | have_increasing <- have_increasing || ((col_v[[1]] + 1e-2) < col_v[[n_f_rows]]) |
78 | 87 | have_decreasing <- have_decreasing || ((col_v[[1]] - 1e-2) > col_v[[n_f_rows]]) |
79 | | - for(col2 in colnames(frames)) { |
| 88 | + for(col2 in colnames(frame)) { |
80 | 89 | if(col2 != 'lambda') { |
81 | | - col2_v <- frames[[col2]] |
| 90 | + col2_v <- frame[[col2]] |
82 | 91 | see_cross <- (abs(col_v[[1]] - col2_v[[1]]) > 1e-2) && (abs(col_v[[n_f_rows]] - col2_v[[n_f_rows]]) > 1e-2) && (col_v[[n_f_rows]] != col2_v[[n_f_rows]]) && ((col_v[[1]] > col2_v[[1]]) != (col_v[[n_f_rows]] > col2_v[[n_f_rows]])) |
83 | 92 | have_cross <- have_cross || see_cross |
84 | 93 | } |
85 | 94 | } |
86 | 95 | } |
87 | 96 | } |
88 | | - if(have_increasing && have_decreasing && have_cross) { |
| 97 | + return(have_increasing && have_decreasing && have_cross) |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +``` r |
| 102 | +mk_example <- function() { |
| 103 | + d_cols <- c('x1', 'x2', 'x3', 'x4', 'y') |
| 104 | + n_rows <- 10 |
| 105 | + while(TRUE) { |
| 106 | + d_ex <- data.frame(matrix( |
| 107 | + sample(seq(-5, 5), size = length(d_cols) * n_rows, replace = TRUE), |
| 108 | + ncol=length(d_cols))) |
| 109 | + colnames(d_ex) <- d_cols |
| 110 | + d_ex['y'] = d_ex['y'] >= 0 |
| 111 | + frame <- build_traj(d_ex) |
| 112 | + if(want_frame(frame)) { |
89 | 113 | return(d_ex) |
90 | 114 | } |
91 | 115 | } |
@@ -97,21 +121,23 @@ d_ex <- mk_example() |
97 | 121 | knitr::kable(d_ex) |
98 | 122 | ``` |
99 | 123 |
|
100 | | -| x1 | x2 | x3 | y | |
101 | | -|----:|----:|----:|:------| |
102 | | -| -2 | -5 | -5 | TRUE | |
103 | | -| -4 | -2 | 0 | TRUE | |
104 | | -| -2 | 0 | -1 | TRUE | |
105 | | -| 3 | 2 | 2 | FALSE | |
106 | | -| -5 | -5 | 3 | FALSE | |
107 | | -| 1 | 0 | -2 | FALSE | |
108 | | -| 2 | -4 | 2 | FALSE | |
109 | | -| 1 | -1 | -3 | FALSE | |
| 124 | +| x1 | x2 | x3 | x4 | y | |
| 125 | +|----:|----:|----:|----:|:------| |
| 126 | +| -4 | 4 | 0 | -2 | TRUE | |
| 127 | +| 3 | -5 | -1 | -1 | TRUE | |
| 128 | +| 1 | -3 | -1 | -3 | TRUE | |
| 129 | +| -5 | -2 | -3 | -5 | TRUE | |
| 130 | +| 4 | -3 | 1 | -5 | FALSE | |
| 131 | +| -1 | 3 | 2 | -3 | TRUE | |
| 132 | +| 2 | 2 | -2 | -2 | FALSE | |
| 133 | +| -5 | 3 | -3 | 5 | TRUE | |
| 134 | +| 5 | 5 | -1 | -1 | FALSE | |
| 135 | +| -2 | -5 | 2 | -4 | FALSE | |
110 | 136 |
|
111 | 137 | ``` r |
112 | | -frames <- build_traj(d_ex) |
| 138 | +frame <- build_traj(d_ex) |
113 | 139 | plot_dat <- data.frame(data.table::melt( |
114 | | - data.table(frames), |
| 140 | + data.table(frame), |
115 | 141 | variable.name = 'row', |
116 | 142 | value.name = 'score', |
117 | 143 | id.vars='lambda')) |
@@ -125,4 +151,4 @@ ggplot( |
125 | 151 | geom_line() |
126 | 152 | ``` |
127 | 153 |
|
128 | | -<!-- --> |
| 154 | +<!-- --> |