Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / 3way.r
Created March 19, 2020 14:24
Three-way contingency table
> table(mtcars$cyl, mtcars$carb, mtcars$gear)
, , = 3
1 2 3 4 6 8
4 1 0 0 0 0 0
6 2 0 0 0 0 0
8 0 4 3 5 0 0
, , = 4
@NyaGarcia
NyaGarcia / cramerv.r
Last active March 19, 2020 15:20
Calculating strength of association with Cramer's V
> CramerV(mtcars$gear, mtcars$cyl)
[1] 0.5308655
@NyaGarcia
NyaGarcia / cramerv-correct.r
Created March 19, 2020 15:09
Calculating measures of association with Cramer's V, with bias correction applied
> CramerV(mtcars$gear, mtcars$cyl, correct = TRUE)
[1] 0.4819631
@NyaGarcia
NyaGarcia / tschuprow-correct.r
Created March 19, 2020 15:17
Calculating strength of association with Tschuprow's T, with bias correction applied
> TschuprowT(mtcars$gear, mtcars$cyl, correct = TRUE)
[1] 0.4819631
@NyaGarcia
NyaGarcia / tschuprow.r
Created March 19, 2020 15:20
Calculating strength of association with Tschuprow's T
> TschuprowT(mtcars$gear, mtcars$cyl)
[1] 0.5308655
@NyaGarcia
NyaGarcia / phi.r
Created March 19, 2020 17:23
Calculating strength of association with the Phi Coefficient
> jobhappiness<-matrix(c(5,7,13,10), ncol = 2)
> rownames(jobhappiness)<-c("Developers", "Mathematicians")
> colnames(jobhappiness)<-c("Happy", "Unhappy")
> phi(jobhappiness)
[1] -0.14
@NyaGarcia
NyaGarcia / duplicated-logic.ts
Last active April 4, 2021 10:45
Duplicated logic in Observables
import { from } from "rxjs";
import { filter, reduce } from "rxjs/operators";
const number$ = from([null, 2, 1, undefined, 5, false, 6, 7]);
// Adding numbers
number$
.pipe(
filter<number>(Boolean),
reduce((acc, curr) => acc + curr)
@NyaGarcia
NyaGarcia / fix-duplicated-logic.ts
Last active April 4, 2021 10:46
Fixing duplicated logic smell by attaching takeUntil() to the source Observable
import { from } from "rxjs";
import { filter, reduce } from "rxjs/operators";
const number$ = from([null, 2, 1, 0, 5, false, 6, 7]).pipe(
filter<number>(Boolean)
);
// Adding numbers
number$
.pipe(
@NyaGarcia
NyaGarcia / exposed-subjects.ts
Created March 20, 2020 15:07
Exposing subjects to the outside world
class DataService {
pokemonLevel$ = new BehaviorSubject<number>(1);
stop$: Subject<void> = new Subject();
number$ = interval(1000).pipe(takeUntil(this.stop$));
}
@NyaGarcia
NyaGarcia / encapsulated-subjects.ts
Created March 20, 2020 15:13
Preserving class encapsulation by providing methods to interact with Subjects
class DataService {
private pokemonLevel = new BehaviorSubject<number>(1);
private stop$: Subject<void> = new Subject();
pokemonLevel$ = this.pokemonLevel.asObservable();
increaseLevel(level: number) {
if (!this.isValidLevel(level)) {
throw new Error("Level is not valid");
}