Skip to content

Instantly share code, notes, and snippets.

Avatar

Hause Lin hauselin

View GitHub Profile
@hauselin
hauselin / github.bash
Created Oct 17, 2020 — forked from igrigorik/github.bash
Open GitHub URL for current directory/repo...
View github.bash
alias gh="open \`git remote -v | grep git@github.com | grep fetch | head -1 | cut -f2 | cut -d' ' -f1 | sed -e's/:/\//' -e 's/git@/http:\/\//'\`"
View gist:c21dc322c67549afed56e2ccd6500d50
df_long = pd.DataFrame(
{"student": ["Andy", "Bernie", "Cindy", "Deb", "Andy", "Bernie", "Cindy", "Deb", "Andy", "Bernie", "Cindy", "Deb"],
"school": ["Z", "Y", "Z", "Y", "Z", "Y", "Z", "Y", "Z", "Y", "Z", "Y"],
"class": ["english", "english", "english", "english", "math", "math", "math", "math", "physics", "physics", "physics", "physics"],
"grade": [10, 100, 1000, 10000, 20, 200, 2000, 20000, 30, 300, 3000, 30000]
}
)
df_long
> student school class grade
View simulate_attract_personality.py
def simulate(simulations=1000, n=500, quantile=0.80):
correlations = np.zeros(simulations)
for i in range(simulations):
data = {"personality": np.random.randn(
n), "attract": np.random.randn(n)}
df = pd.DataFrame(data)
View summarySEwithin2.R
# see also seWithin function in hausekeep package
# https://hauselin.github.io/hausekeep/reference/seWithin.html
summarySE2 <- function (data = NULL, measurevar, groupvars = NULL, na.rm = TRUE, conf.interval = 0.95) {
library(data.table)
data <- data.table(data)
length2 <- function(x, na.rm = FALSE) {
if (na.rm)
sum(!is.na(x))
else length(x)
View summarySE2
summarySE2 <- function (data = NULL, measurevar, groupvars = NULL, na.rm = TRUE, conf.interval = 0.95) {
library(data.table)
data <- data.table(data)
length2 <- function(x, na.rm = FALSE) {
if (na.rm)
sum(!is.na(x))
else length(x)
}
View logisticRegressionR2s.R
#function to compute R2s in logistic regression
logisticR2s <- function(logisticModel) {
modelDeviance <- logisticModel$deviance
nullDeviance <- logisticModel$null.deviance
n <- length(logisticModel$fitted.values)
R2HosmerLemeshow <- 1 - modelDeviance / nullDeviance
R2CoxSnell <- 1 - exp((modelDeviance - nullDeviance) / n)
R2Nagelkerke <- R2CoxSnell / (1 - exp( - (nullDeviance / n)))
View gist:5f74ce5251521b72df61
data <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L,
8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 14L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L,
16L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 19L, 19L,
19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L,
22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L,
24L, 24L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 27L,
@hauselin
hauselin / gist:299fc906e3de49b73cb2
Last active May 12, 2016
R functions for Qualtrics
View gist:299fc906e3de49b73cb2
#function to read raw data from Qualtrics
cleanQualtrics <- function(csvFile, rowAsHeader, skipRows) {
#this function assumes that you have named your
#Qualtrics questions properly when setting up the survey;
#if questions are properly named, then the first row
#will be most informative and suitable for use as column names
#read.csv sets header = T by default; stringsAsFactor set to FALSE to ensure strings aren't converted to factors
QualtricsRaw <- read.csv(csvFile, header = F, stringsAsFactors = F)
#row 1 contains the strings that we'd like to use as column names; select row 1 and turn them into characters
colNames <- as.character(QualtricsRaw[rowAsHeader,])