GeezerVison is showing github.com — more examples · WinVector/Examples@e159ee5
File treeExpand file treeCollapse file tree Open diff view settings traj_graph_2_files/figure-gfm
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: rebalance/traj_graph_2.Rmd +74-36Lines changed: 74 additions & 36 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,24 +7,74 @@ date: "2024-06-23" | 7 | 7 | ```{r} | 8 | 8 | library(ggplot2) | 9 | 9 | library(data.table) | | 10 | +library(wrapr) | 10 | 11 | set.seed(2024) | 11 | 12 | ``` | 12 | 13 |
| 13 | 14 |
| | 15 | + | | 16 | + | | 17 | +```{r} | | 18 | +build_traj <- function(d_ex, lambda_max = 3, length.out = 100) { | | 19 | + frame <- list() | | 20 | + model_coefs <- NULL | | 21 | + suppressWarnings( | | 22 | + for(lambda in seq(0, lambda_max, length.out = length.out)) { | | 23 | + model <- glm(y ~ x1 + x2, family=binomial(), data=d_ex, weights = 1 + lambda * d_ex$y) | | 24 | + model_coefs <- coef(model) | | 25 | + preds <- predict(model, type='response', newdata=d_ex) | | 26 | + row_frame <- as.data.frame(t(as.matrix(preds))) | | 27 | + row_frame['lambda'] <- lambda | | 28 | + frame <- c(frame, list(row_frame)) | | 29 | + } | | 30 | + ) | | 31 | + frame <- do.call(rbind, frame) | | 32 | + return(list(frame=frame, model_coefs=model_coefs)) | | 33 | +} | | 34 | +``` | | 35 | + | | 36 | + | 14 | 37 | ```{r} | 15 | | -d_ex <- wrapr::build_frame( | | 38 | +plot_traj_graph <- function(d_ex, ..., lambda_max = 3, title = '') { | | 39 | + to[frame, model_coefs] <- build_traj(d_ex, lambda_max = lambda_max) | | 40 | + plot_dat <- data.frame(data.table::melt( | | 41 | + data.table(frame), | | 42 | + variable.name = 'row', | | 43 | + value.name = 'score', | | 44 | + id.vars='lambda')) | | 45 | + g <- ggplot( | | 46 | + data=plot_dat, | | 47 | + mapping=aes(x=lambda, y=score, color=row, shape=row) | | 48 | + ) + | | 49 | + geom_line(alpha=0.5) + | | 50 | + geom_point(alpha=0.5) + | | 51 | + ggtitle(title) | | 52 | + return(list(plot=g, model_coefs=model_coefs)) | | 53 | +} | | 54 | +``` | | 55 | + | | 56 | + | | 57 | + | | 58 | +```{r} | | 59 | +d_ex_1 <- wrapr::build_frame( | 16 | 60 | "x1" , "x2", "y" | | 17 | 61 | 0 , 0 , 0 | | 18 | 62 | 1 , 0 , 1 | | 19 | | - -2 , 0 , 0 | | | 63 | + -2 , 0 , 0 | | 20 | 64 | 0 , 2 , 1 | | 21 | 65 | 1 , 2 , 1 | | 22 | 66 | 1 , 1 , 0 ) | 23 | 67 | ``` | 24 | 68 |
| | 69 | +```{r} | | 70 | +to[plot, model_coefs] <- plot_traj_graph(d_ex_1, title='ex_1') | | 71 | +print(model_coefs) | | 72 | +plot | | 73 | +``` | | 74 | + | 25 | 75 |
| 26 | 76 | ```{r} | 27 | | - d_ex <- wrapr::build_frame( | | 77 | + d_ex_2 <- wrapr::build_frame( | 28 | 78 | "x1" , "x2", "x3", "x4", "y" | | 29 | 79 | -4L , 4L , 0L , -2L , TRUE | | 30 | 80 | 3L , -5L , -1L , -1L , TRUE | | @@ -39,32 +89,31 @@ d_ex <- wrapr::build_frame( | 39 | 89 | ``` | 40 | 90 |
| 41 | 91 |
| 42 | | - | 43 | 92 | ```{r} | 44 | | -build_traj <- function(d_ex, lambda_max = 3, length.out = 100) { | 45 | | - frame <- list() | 46 | | - suppressWarnings( | 47 | | - for(lambda in seq(0, lambda_max, length.out = length.out)) { | 48 | | - model <- glm(y ~ x1 + x2, family=binomial(), data=d_ex, weights = 1 + lambda * d_ex$y) | 49 | | - preds <- predict(model, type='response', newdata=d_ex) | 50 | | - row_frame <- as.data.frame(t(as.matrix(preds))) | 51 | | - row_frame['lambda'] <- lambda | 52 | | - frame <- c(frame, list(row_frame)) | 53 | | - } | 54 | | - ) | 55 | | - frame <- do.call(rbind, frame) | 56 | | - return(frame) | 57 | | -} | | 93 | +to[plot, model_coefs] <- plot_traj_graph(d_ex_2, title='ex_2') | | 94 | +print(model_coefs) | | 95 | +plot | 58 | 96 | ``` | 59 | 97 |
| 60 | 98 |
| | 99 | + | 61 | 100 | ```{r} | 62 | | -logit <- function(x) {log(x/(1-x))} | | 101 | +d_ex_3 <- wrapr::build_frame( | | 102 | + "x1" , "x2", "y" | | | 103 | + 0 , 0 , 0 | | | 104 | + 1 , 0 , 1 | | | 105 | + 0 , 1 , 1 | | | 106 | + 1 , 1 , 0 | | | 107 | + -1 , 1 , 0 | | | 108 | + 1 , -1 , 0 ) | 63 | 109 | ``` | 64 | 110 |
| 65 | 111 |
| | 112 | + | 66 | 113 | ```{r} | 67 | | -frame <- build_traj(d_ex) | | 114 | +to[plot, model_coefs] <- plot_traj_graph(d_ex_3, lambda_max = 1000, title='ex_3') | | 115 | +print(model_coefs) | | 116 | +plot | 68 | 117 | ``` | 69 | 118 |
| 70 | 119 |
| @@ -108,7 +157,7 @@ mk_example <- function() { | 108 | 157 | ncol=length(d_cols))) | 109 | 158 | colnames(d_ex) <- d_cols | 110 | 159 | d_ex['y'] = d_ex['y'] >= 0 | 111 | | - frame <- build_traj(d_ex) | | 160 | + to[frame, model_coefs] <- build_traj(d_ex) | 112 | 161 | if(want_frame(frame)) { | 113 | 162 | return(d_ex) | 114 | 163 | } | @@ -130,25 +179,14 @@ cat(wrapr::draw_frame(d_ex)) | 130 | 179 | knitr::kable(d_ex) | 131 | 180 | ``` | 132 | 181 |
| 133 | | -```{r} | 134 | | -frame <- build_traj(d_ex) | 135 | | -plot_dat <- data.frame(data.table::melt( | 136 | | - data.table(frame), | 137 | | - variable.name = 'row', | 138 | | - value.name = 'score', | 139 | | - id.vars='lambda')) | 140 | | -``` | 141 | | - | 142 | | - | 143 | 182 |
| 144 | 183 | ```{r} | 145 | | -ggplot( | 146 | | - data=plot_dat, | 147 | | - mapping=aes(x=lambda, y=score, color=row) | 148 | | -) + | 149 | | - geom_line() | | 184 | +to[plot, model_coefs] <- plot_traj_graph(d_ex, title='ex') | | 185 | +print(model_coefs) | | 186 | +plot | 150 | 187 | ``` | 151 | 188 |
| | 189 | + | 152 | 190 | ```{r} | 153 | 191 | w_1 <- rep(1, nrow(d_ex)) | 154 | 192 | model_1 <- glm( | |
Collapse file Copy file name to clipboardExpand all lines: rebalance/traj_graph_2.md +120-32Lines changed: 120 additions & 32 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -14,23 +14,86 @@ library(data.table) | 14 | 14 |
| 15 | 15 | ## Warning: package 'data.table' was built under R version 4.3.2 | 16 | 16 |
| | 17 | +``` r | | 18 | +library(wrapr) | | 19 | +``` | | 20 | + | | 21 | + ## | | 22 | + ## Attaching package: 'wrapr' | | 23 | + | | 24 | + ## The following objects are masked from 'package:data.table': | | 25 | + ## | | 26 | + ## :=, let | | 27 | + | 17 | 28 | ``` r | 18 | 29 | set.seed(2024) | 19 | 30 | ``` | 20 | 31 |
| 21 | 32 | ``` r | 22 | | -d_ex <- wrapr::build_frame( | | 33 | +build_traj <- function(d_ex, lambda_max = 3, length.out = 100) { | | 34 | + frame <- list() | | 35 | + model_coefs <- NULL | | 36 | + suppressWarnings( | | 37 | + for(lambda in seq(0, lambda_max, length.out = length.out)) { | | 38 | + model <- glm(y ~ x1 + x2, family=binomial(), data=d_ex, weights = 1 + lambda * d_ex$y) | | 39 | + model_coefs <- coef(model) | | 40 | + preds <- predict(model, type='response', newdata=d_ex) | | 41 | + row_frame <- as.data.frame(t(as.matrix(preds))) | | 42 | + row_frame['lambda'] <- lambda | | 43 | + frame <- c(frame, list(row_frame)) | | 44 | + } | | 45 | + ) | | 46 | + frame <- do.call(rbind, frame) | | 47 | + return(list(frame=frame, model_coefs=model_coefs)) | | 48 | +} | | 49 | +``` | | 50 | + | | 51 | +``` r | | 52 | +plot_traj_graph <- function(d_ex, ..., lambda_max = 3, title = '') { | | 53 | + to[frame, model_coefs] <- build_traj(d_ex, lambda_max = lambda_max) | | 54 | + plot_dat <- data.frame(data.table::melt( | | 55 | + data.table(frame), | | 56 | + variable.name = 'row', | | 57 | + value.name = 'score', | | 58 | + id.vars='lambda')) | | 59 | + g <- ggplot( | | 60 | + data=plot_dat, | | 61 | + mapping=aes(x=lambda, y=score, color=row, shape=row) | | 62 | + ) + | | 63 | + geom_line(alpha=0.5) + | | 64 | + geom_point(alpha=0.5) + | | 65 | + ggtitle(title) | | 66 | + return(list(plot=g, model_coefs=model_coefs)) | | 67 | +} | | 68 | +``` | | 69 | + | | 70 | +``` r | | 71 | +d_ex_1 <- wrapr::build_frame( | 23 | 72 | "x1" , "x2", "y" | | 24 | 73 | 0 , 0 , 0 | | 25 | 74 | 1 , 0 , 1 | | 26 | | - -2 , 0 , 0 | | | 75 | + -2 , 0 , 0 | | 27 | 76 | 0 , 2 , 1 | | 28 | 77 | 1 , 2 , 1 | | 29 | 78 | 1 , 1 , 0 ) | 30 | 79 | ``` | 31 | 80 |
| 32 | 81 | ``` r | 33 | | - d_ex <- wrapr::build_frame( | | 82 | +to[plot, model_coefs] <- plot_traj_graph(d_ex_1, title='ex_1') | | 83 | +print(model_coefs) | | 84 | +``` | | 85 | + | | 86 | + ## (Intercept) x1 x2 | | 87 | + ## -0.563986 1.882809 1.378345 | | 88 | + | | 89 | +``` r | | 90 | +plot | | 91 | +``` | | 92 | + | | 93 | +<!-- --> | | 94 | + | | 95 | +``` r | | 96 | + d_ex_2 <- wrapr::build_frame( | 34 | 97 | "x1" , "x2", "x3", "x4", "y" | | 35 | 98 | -4L , 4L , 0L , -2L , TRUE | | 36 | 99 | 3L , -5L , -1L , -1L , TRUE | | @@ -45,30 +108,52 @@ d_ex <- wrapr::build_frame( | 45 | 108 | ``` | 46 | 109 |
| 47 | 110 | ``` r | 48 | | -build_traj <- function(d_ex, lambda_max = 3, length.out = 100) { | 49 | | - frame <- list() | 50 | | - suppressWarnings( | 51 | | - for(lambda in seq(0, lambda_max, length.out = length.out)) { | 52 | | - model <- glm(y ~ x1 + x2, family=binomial(), data=d_ex, weights = 1 + lambda * d_ex$y) | 53 | | - preds <- predict(model, type='response', newdata=d_ex) | 54 | | - row_frame <- as.data.frame(t(as.matrix(preds))) | 55 | | - row_frame['lambda'] <- lambda | 56 | | - frame <- c(frame, list(row_frame)) | 57 | | - } | 58 | | - ) | 59 | | - frame <- do.call(rbind, frame) | 60 | | - return(frame) | 61 | | -} | | 111 | +to[plot, model_coefs] <- plot_traj_graph(d_ex_2, title='ex_2') | | 112 | +print(model_coefs) | | 113 | +``` | | 114 | + | | 115 | + ## (Intercept) x1 x2 | | 116 | + ## 1.8930684 -0.5586554 -0.1716650 | | 117 | + | | 118 | +``` r | | 119 | +plot | 62 | 120 | ``` | 63 | 121 |
| | 122 | + ## Warning: The shape palette can deal with a maximum of 6 discrete values because more | | 123 | + ## than 6 becomes difficult to discriminate | | 124 | + ## ℹ you have requested 10 values. Consider specifying shapes manually if you need | | 125 | + ## that many have them. | | 126 | + | | 127 | + ## Warning: Removed 400 rows containing missing values or values outside the scale range | | 128 | + ## (`geom_point()`). | | 129 | + | | 130 | +<!-- --> | | 131 | + | 64 | 132 | ``` r | 65 | | -logit <- function(x) {log(x/(1-x))} | | 133 | +d_ex_3 <- wrapr::build_frame( | | 134 | + "x1" , "x2", "y" | | | 135 | + 0 , 0 , 0 | | | 136 | + 1 , 0 , 1 | | | 137 | + 0 , 1 , 1 | | | 138 | + 1 , 1 , 0 | | | 139 | + -1 , 1 , 0 | | | 140 | + 1 , -1 , 0 ) | 66 | 141 | ``` | 67 | 142 |
| 68 | 143 | ``` r | 69 | | -frame <- build_traj(d_ex) | | 144 | +to[plot, model_coefs] <- plot_traj_graph(d_ex_3, lambda_max = 1000, title='ex_3') | | 145 | +print(model_coefs) | 70 | 146 | ``` | 71 | 147 |
| | 148 | + ## (Intercept) x1 x2 | | 149 | + ## -0.6931479 7.6009037 7.6009037 | | 150 | + | | 151 | +``` r | | 152 | +plot | | 153 | +``` | | 154 | + | | 155 | +<!-- --> | | 156 | + | 72 | 157 | ``` r | 73 | 158 | want_frame <- function(frame) { | 74 | 159 | check_eps = 0.03 | @@ -106,7 +191,7 @@ mk_example <- function() { | 106 | 191 | ncol=length(d_cols))) | 107 | 192 | colnames(d_ex) <- d_cols | 108 | 193 | d_ex['y'] = d_ex['y'] >= 0 | 109 | | - frame <- build_traj(d_ex) | | 194 | + to[frame, model_coefs] <- build_traj(d_ex) | 110 | 195 | if(want_frame(frame)) { | 111 | 196 | return(d_ex) | 112 | 197 | } | @@ -153,23 +238,26 @@ knitr::kable(d_ex) | 153 | 238 | | -12 | 14 | TRUE | | 154 | 239 |
| 155 | 240 | ``` r | 156 | | -frame <- build_traj(d_ex) | 157 | | -plot_dat <- data.frame(data.table::melt( | 158 | | - data.table(frame), | 159 | | - variable.name = 'row', | 160 | | - value.name = 'score', | 161 | | - id.vars='lambda')) | | 241 | +to[plot, model_coefs] <- plot_traj_graph(d_ex, title='ex') | | 242 | +print(model_coefs) | 162 | 243 | ``` | 163 | 244 |
| | 245 | + ## (Intercept) x1 x2 | | 246 | + ## 1.15381279 -0.12114995 -0.06336498 | | 247 | + | 164 | 248 | ``` r | 165 | | -ggplot( | 166 | | - data=plot_dat, | 167 | | - mapping=aes(x=lambda, y=score, color=row) | 168 | | -) + | 169 | | - geom_line() | | 249 | +plot | 170 | 250 | ``` | 171 | 251 |
| 172 | | -<!-- --> | | 252 | + ## Warning: The shape palette can deal with a maximum of 6 discrete values because more | | 253 | + ## than 6 becomes difficult to discriminate | | 254 | + ## ℹ you have requested 10 values. Consider specifying shapes manually if you need | | 255 | + ## that many have them. | | 256 | + | | 257 | + ## Warning: Removed 400 rows containing missing values or values outside the scale range | | 258 | + ## (`geom_point()`). | | 259 | + | | 260 | +<!-- --> | 173 | 261 |
| 174 | 262 | ``` r | 175 | 263 | w_1 <- rep(1, nrow(d_ex)) | |
Collapse file Copy file name to clipboard -6.89 KB
- Display the source diff
- Display the rich diff
Collapse file Copy file name to clipboard 87.6 KB
- Display the source diff
- Display the rich diff
Collapse file Copy file name to clipboard 87.6 KB
- Display the source diff
- Display the rich diff
Collapse file Copy file name to clipboard 31.8 KB
- Display the source diff
- Display the rich diff
Collapse file Copy file name to clipboard 66.1 KB
- Display the source diff
- Display the rich diff
Collapse file Copy file name to clipboard 2.19 KB
- Display the source diff
- Display the rich diff
|