Skip to content

Instantly share code, notes, and snippets.

@gireeshkbogu
Last active January 22, 2016 10:51
Show Gist options
  • Save gireeshkbogu/b7d99a34644d05a5ccaa to your computer and use it in GitHub Desktop.
Save gireeshkbogu/b7d99a34644d05a5ccaa to your computer and use it in GitHub Desktop.
generate_logit_cdf <- function(mu, s,
sigma_y=0.1,
x=seq(-5,20,0.1)) {
x_ms <- (x-mu)/s
y <- 0.5 + 0.5 * tanh(x_ms)
y <- abs(y + rnorm(length(x), 0, sigma_y))
ix <- which(y>=1.0)
if(length(ix)>=1) {
y[ix] <- 1.0
}
return(y)
}
set.seed(424242)
x <- seq(-5,20,0.025) # 1001 observation
mu_vec <- c(1,2,3,5,7,8) # 6 variables
s_vec <- c(2,2,4,3,4,2)
# Syntetic variables
observations_df<- mapply(generate_logit_cdf,
mu_vec,
s_vec,
MoreArgs = list(x=x))
# Give them names
colnames(observations_df) <- c("Var1", "Var2", "Var3", "Var4", "Var5", "Var6")
> head(observations_df)
Var1 Var2 Var3 Var4 Var5 Var6
[1,] 0.04646783 0.178231019 0.07197742 0.11099645 0.06384057 0.02629486
[2,] 0.09946289 0.081685028 0.18240798 0.06993340 0.10113648 0.04727540
[3,] 0.07871231 0.029549890 0.08254837 0.09690864 0.08558282 0.16012873
[4,] 0.01986314 0.060743272 0.18354161 0.17263607 0.04554137 0.03000444
[5,] 0.04518787 0.029594108 0.02928619 0.04532478 0.02640770 0.09849672
[6,] 0.22511967 0.007850177 0.08122381 0.13976551 0.05582895 0.04619664
> head(df_all)
x observation y
1 1 Var1 0.04646783
2 2 Var1 0.09946289
3 3 Var1 0.07871231
4 4 Var1 0.01986314
5 5 Var1 0.04518787
6 6 Var1 0.22511967
library(ggplot2)
library(reshape2)
df_all <- reshape2:::melt(observations_df)
colnames(df_all) <- c("x", "observation", "y")
df_all$observation <- as.factor(df_all$observation)
ggplot(df_all, aes(x=x, y=y, colour=observation)) + geom_point(size=3) + scale_color_brewer(palette = "Reds") + geom_smooth(aes(group=observation),method="glm", family=quasibinomial(), formula="y~x", se = FALSE, size=1.5)
#cumulative plot:
#https://cloud.githubusercontent.com/assets/3885659/12447432/6f1b296c-bf70-11e5-82d8-cb105beafc17.png
#useful links:
#fitting models - https://www.zoology.ubc.ca/~schluter/R/fit-model/
#http://www.r-bloggers.com/s-shaped-data-smoothing-with-quasibinomial-distribution/
@gireeshkbogu
Copy link
Author

cumulative distribution plots

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment