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 — more examples · WinVector/Examples@e159ee5

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-11-1.png
      • unnamed-chunk-15-1.png
      • unnamed-chunk-17-1.png
      • unnamed-chunk-5-1.png
      • unnamed-chunk-7-1.png
      • unnamed-chunk-9-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
+74-36Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,74 @@ date: "2024-06-23"
77
```{r}
88
library(ggplot2)
99
library(data.table)
10+
library(wrapr)
1011
set.seed(2024)
1112
```
1213

1314

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+
1437
```{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(
1660
"x1" , "x2", "y" |
1761
0 , 0 , 0 |
1862
1 , 0 , 1 |
19-
-2 , 0 , 0 |
63+
-2 , 0 , 0 |
2064
0 , 2 , 1 |
2165
1 , 2 , 1 |
2266
1 , 1 , 0 )
2367
```
2468

69+
```{r}
70+
to[plot, model_coefs] <- plot_traj_graph(d_ex_1, title='ex_1')
71+
print(model_coefs)
72+
plot
73+
```
74+
2575

2676
```{r}
27-
d_ex <- wrapr::build_frame(
77+
d_ex_2 <- wrapr::build_frame(
2878
"x1" , "x2", "x3", "x4", "y" |
2979
-4L , 4L , 0L , -2L , TRUE |
3080
3L , -5L , -1L , -1L , TRUE |
@@ -39,32 +89,31 @@ d_ex <- wrapr::build_frame(
3989
```
4090

4191

42-
4392
```{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
5896
```
5997

6098

99+
61100
```{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 )
63109
```
64110

65111

112+
66113
```{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
68117
```
69118

70119

@@ -108,7 +157,7 @@ mk_example <- function() {
108157
ncol=length(d_cols)))
109158
colnames(d_ex) <- d_cols
110159
d_ex['y'] = d_ex['y'] >= 0
111-
frame <- build_traj(d_ex)
160+
to[frame, model_coefs] <- build_traj(d_ex)
112161
if(want_frame(frame)) {
113162
return(d_ex)
114163
}
@@ -130,25 +179,14 @@ cat(wrapr::draw_frame(d_ex))
130179
knitr::kable(d_ex)
131180
```
132181

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

144183
```{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
150187
```
151188

189+
152190
```{r}
153191
w_1 <- rep(1, nrow(d_ex))
154192
model_1 <- glm(
Collapse file

‎rebalance/traj_graph_2.md‎

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 numberDiff line numberDiff line change
@@ -14,23 +14,86 @@ library(data.table)
1414

1515
## Warning: package 'data.table' was built under R version 4.3.2
1616

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+
1728
``` r
1829
set.seed(2024)
1930
```
2031

2132
``` 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(
2372
"x1" , "x2", "y" |
2473
0 , 0 , 0 |
2574
1 , 0 , 1 |
26-
-2 , 0 , 0 |
75+
-2 , 0 , 0 |
2776
0 , 2 , 1 |
2877
1 , 2 , 1 |
2978
1 , 1 , 0 )
3079
```
3180

3281
``` 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+
![](traj_graph_2_files/figure-gfm/unnamed-chunk-5-1.png)<!-- -->
94+
95+
``` r
96+
d_ex_2 <- wrapr::build_frame(
3497
"x1" , "x2", "x3", "x4", "y" |
3598
-4L , 4L , 0L , -2L , TRUE |
3699
3L , -5L , -1L , -1L , TRUE |
@@ -45,30 +108,52 @@ d_ex <- wrapr::build_frame(
45108
```
46109

47110
``` 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
62120
```
63121

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+
![](traj_graph_2_files/figure-gfm/unnamed-chunk-7-1.png)<!-- -->
131+
64132
``` 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 )
66141
```
67142

68143
``` 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)
70146
```
71147

148+
## (Intercept) x1 x2
149+
## -0.6931479 7.6009037 7.6009037
150+
151+
``` r
152+
plot
153+
```
154+
155+
![](traj_graph_2_files/figure-gfm/unnamed-chunk-9-1.png)<!-- -->
156+
72157
``` r
73158
want_frame <- function(frame) {
74159
check_eps = 0.03
@@ -106,7 +191,7 @@ mk_example <- function() {
106191
ncol=length(d_cols)))
107192
colnames(d_ex) <- d_cols
108193
d_ex['y'] = d_ex['y'] >= 0
109-
frame <- build_traj(d_ex)
194+
to[frame, model_coefs] <- build_traj(d_ex)
110195
if(want_frame(frame)) {
111196
return(d_ex)
112197
}
@@ -153,23 +238,26 @@ knitr::kable(d_ex)
153238
| -12 | 14 | TRUE |
154239

155240
``` 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)
162243
```
163244

245+
## (Intercept) x1 x2
246+
## 1.15381279 -0.12114995 -0.06336498
247+
164248
``` r
165-
ggplot(
166-
data=plot_dat,
167-
mapping=aes(x=lambda, y=score, color=row)
168-
) +
169-
geom_line()
249+
plot
170250
```
171251

172-
![](traj_graph_2_files/figure-gfm/unnamed-chunk-13-1.png)<!-- -->
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+
![](traj_graph_2_files/figure-gfm/unnamed-chunk-15-1.png)<!-- -->
173261

174262
``` r
175263
w_1 <- rep(1, nrow(d_ex))
Collapse file

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

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

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

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

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

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

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

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

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

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

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

Copy file name to clipboard
2.19 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.