Created
June 17, 2022 05:57
-
-
Save coolbutuseless/28382fb522fa716b114a27d010ddc6ac to your computer and use it in GitHub Desktop.
Game of Fits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# Use x11() device as Rstudio device has "issues" with grid.locator() | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
library(grid) | |
x11(type = 'cairo') | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# Some linear-ish points in range/domain [0,1] | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
N <- 10 | |
y <- seq(0, 1, length.out = N) * runif(N) | |
x <- seq(0, 1, length.out = N) | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# plot the points | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
grid.newpage() | |
grid.points(x, y, default.units = 'npc') | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# Ask the user for 2 end points for their guess | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
p1 <- unlist(grid.locator(unit = 'npc')) | |
p2 <- unlist(grid.locator(unit = 'npc')) | |
x1 <- p1[1]; y1 <- p1[2] | |
x2 <- p2[1]; y2 <- p2[2] | |
grid.lines(x = c(x1, x2), y = c(y1, y2), gp = gpar(col='red')) | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# Draw the actual fit | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
fit <- lm(y ~ x, data = data.frame(x=x, y=y)) | |
y1 <- fit$coefficients[1] | |
y2 <- fit$coefficients[1] + fit$coefficients[2] | |
grid.lines(c(0, 1), c(y1, y2), gp = gpar(col = 'blue')) | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# Calculate a score for this fit vs the users fit. | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# Someone else can write this if they wish :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment