GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — working on examples · WinVector/Examples@f705355

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • rebalance
    • traj_graph_2.Rmd
    • traj_graph_2.md
    • traj_graph_2_files/figure-gfm
      • unnamed-chunk-10-1.png
      • unnamed-chunk-11-1.png
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎rebalance/traj_graph_2.Rmd‎

Copy file name to clipboardExpand all lines: rebalance/traj_graph_2.Rmd
+51-23Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ d_ex <- wrapr::build_frame(
2828

2929
```{r}
3030
build_traj <- function(d_ex, lambda_max = 4, length.out = 100) {
31-
frames <- list()
31+
frame <- list()
3232
suppressWarnings(
3333
for(lambda in seq(0, lambda_max, length.out = length.out)) {
3434
model <- glm(y ~ x1 + x2, family=binomial(), data=d_ex, weights = 1 + lambda * d_ex$y)
3535
preds <- predict(model, type='response', newdata=d_ex)
3636
row_frame <- as.data.frame(t(as.matrix(preds)))
3737
row_frame['lambda'] <- lambda
38-
frames <- c(frames, list(row_frame))
38+
frame <- c(frame, list(row_frame))
3939
}
4040
)
41-
frames <- do.call(rbind, frames)
42-
return(frames)
41+
frame <- do.call(rbind, frame)
42+
return(frame)
4343
}
4444
```
4545

@@ -50,40 +50,68 @@ logit <- function(x) {log(x/(1-x))}
5050

5151

5252
```{r}
53-
frames <- build_traj(d_ex)
53+
frame <- build_traj(d_ex)
5454
```
5555

5656

5757
```{r}
58-
mk_example <- function() {
59-
d_cols <- c('x1', 'x2', 'x3', 'y')
60-
n_rows <- 8
61-
while(TRUE) {
62-
d_ex <- data.frame(matrix(
63-
sample(seq(-5, 5), size = length(d_cols) * n_rows, replace = TRUE),
64-
ncol=length(d_cols)))
65-
colnames(d_ex) <- d_cols
66-
d_ex['y'] = d_ex['y'] >= 0
67-
frames <- build_traj(d_ex)
68-
n_f_rows <- nrow(frames)
58+
want_frame <- function(frame) {
59+
n_f_rows <- nrow(frame)
6960
have_cross <- FALSE
7061
have_increasing <- FALSE
7162
have_decreasing <- FALSE
72-
for(col in colnames(frames)) {
63+
for(col in colnames(frame)) {
7364
if(col != 'lambda') {
74-
col_v <- frames[[col]]
65+
col_v <- frame[[col]]
66+
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]])) ) {
67+
return(TRUE)
68+
}
69+
}
70+
}
71+
return(FALSE)
72+
}
73+
```
74+
75+
76+
```{r}
77+
want_frame <- function(frame) {
78+
n_f_rows <- nrow(frame)
79+
have_cross <- FALSE
80+
have_increasing <- FALSE
81+
have_decreasing <- FALSE
82+
for(col in colnames(frame)) {
83+
if(col != 'lambda') {
84+
col_v <- frame[[col]]
7585
have_increasing <- have_increasing || ((col_v[[1]] + 1e-2) < col_v[[n_f_rows]])
7686
have_decreasing <- have_decreasing || ((col_v[[1]] - 1e-2) > col_v[[n_f_rows]])
77-
for(col2 in colnames(frames)) {
87+
for(col2 in colnames(frame)) {
7888
if(col2 != 'lambda') {
79-
col2_v <- frames[[col2]]
89+
col2_v <- frame[[col2]]
8090
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]]))
8191
have_cross <- have_cross || see_cross
8292
}
8393
}
8494
}
8595
}
86-
if(have_increasing && have_decreasing && have_cross) {
96+
return(have_increasing && have_decreasing && have_cross)
97+
}
98+
```
99+
100+
101+
102+
103+
```{r}
104+
mk_example <- function() {
105+
d_cols <- c('x1', 'x2', 'x3', 'x4', 'y')
106+
n_rows <- 10
107+
while(TRUE) {
108+
d_ex <- data.frame(matrix(
109+
sample(seq(-5, 5), size = length(d_cols) * n_rows, replace = TRUE),
110+
ncol=length(d_cols)))
111+
colnames(d_ex) <- d_cols
112+
d_ex['y'] = d_ex['y'] >= 0
113+
frame <- build_traj(d_ex)
114+
if(want_frame(frame)) {
87115
return(d_ex)
88116
}
89117
}
@@ -98,9 +126,9 @@ knitr::kable(d_ex)
98126
```
99127

100128
```{r}
101-
frames <- build_traj(d_ex)
129+
frame <- build_traj(d_ex)
102130
plot_dat <- data.frame(data.table::melt(
103-
data.table(frames),
131+
data.table(frame),
104132
variable.name = 'row',
105133
value.name = 'score',
106134
id.vars='lambda'))
Collapse file

‎rebalance/traj_graph_2.md‎

Copy file name to clipboardExpand all lines: rebalance/traj_graph_2.md
+60-34Lines changed: 60 additions & 34 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ d_ex <- wrapr::build_frame(
3333

3434
``` r
3535
build_traj <- function(d_ex, lambda_max = 4, length.out = 100) {
36-
frames <- list()
36+
frame <- list()
3737
suppressWarnings(
3838
for(lambda in seq(0, lambda_max, length.out = length.out)) {
3939
model <- glm(y ~ x1 + x2, family=binomial(), data=d_ex, weights = 1 + lambda * d_ex$y)
4040
preds <- predict(model, type='response', newdata=d_ex)
4141
row_frame <- as.data.frame(t(as.matrix(preds)))
4242
row_frame['lambda'] <- lambda
43-
frames <- c(frames, list(row_frame))
43+
frame <- c(frame, list(row_frame))
4444
}
4545
)
46-
frames <- do.call(rbind, frames)
47-
return(frames)
46+
frame <- do.call(rbind, frame)
47+
return(frame)
4848
}
4949
```
5050

@@ -53,39 +53,63 @@ logit <- function(x) {log(x/(1-x))}
5353
```
5454

5555
``` r
56-
frames <- build_traj(d_ex)
56+
frame <- build_traj(d_ex)
5757
```
5858

5959
``` 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)
7162
have_cross <- FALSE
7263
have_increasing <- FALSE
7364
have_decreasing <- FALSE
74-
for(col in colnames(frames)) {
65+
for(col in colnames(frame)) {
7566
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]]
7786
have_increasing <- have_increasing || ((col_v[[1]] + 1e-2) < col_v[[n_f_rows]])
7887
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)) {
8089
if(col2 != 'lambda') {
81-
col2_v <- frames[[col2]]
90+
col2_v <- frame[[col2]]
8291
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]]))
8392
have_cross <- have_cross || see_cross
8493
}
8594
}
8695
}
8796
}
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)) {
89113
return(d_ex)
90114
}
91115
}
@@ -97,21 +121,23 @@ d_ex <- mk_example()
97121
knitr::kable(d_ex)
98122
```
99123

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 |
110136

111137
``` r
112-
frames <- build_traj(d_ex)
138+
frame <- build_traj(d_ex)
113139
plot_dat <- data.frame(data.table::melt(
114-
data.table(frames),
140+
data.table(frame),
115141
variable.name = 'row',
116142
value.name = 'score',
117143
id.vars='lambda'))
@@ -125,4 +151,4 @@ ggplot(
125151
geom_line()
126152
```
127153

128-
![](traj_graph_2_files/figure-gfm/unnamed-chunk-9-1.png)<!-- -->
154+
![](traj_graph_2_files/figure-gfm/unnamed-chunk-11-1.png)<!-- -->
Collapse file

‎rebalance/traj_graph_2_files/figure-gfm/unnamed-chunk-10-1.png‎

Copy file name to clipboard
45.1 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎rebalance/traj_graph_2_files/figure-gfm/unnamed-chunk-11-1.png‎

Copy file name to clipboard
54.2 KB
  • Display the source diff
  • Display the rich diff
Loading

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.