Skip to content

Instantly share code, notes, and snippets.

View ben-domingue's full-sized avatar

Ben Domingue ben-domingue

  • Stanford University
View GitHub Profile
bigf<-function(df) {
df<-df[!is.na(df$rt),]
df<-df[df$rt<60*5,]
x<-irw::long2resp(df)
id<-x$id
x$id<-NULL
m<-mirt::mirt(x,1,'Rasch')
th<-mirt::fscores(m)
th<-data.frame(id=id,th=th[,1])
df<-merge(df,th)
library(TestGardener)
convert<-function(resp) {
n<-ncol(resp)
N<-nrow(resp)
for (i in 1:ncol(resp)) {
vals<-sample(2:4,N,replace=TRUE)
resp[,i]<-ifelse(resp[,i]==1,resp[,i],vals)
}
optList <- list()
grbg <- matrix(0,n,1)
@ben-domingue
ben-domingue / ncme2022_CTTdemo.R
Last active April 18, 2022 20:46
Demonstration of item response simulation that breaks reliability estimates.
##a helper function we'll use along the way.
##kr20 is a function that computes the kr20 version of cronbach's alpha
kr20<-function(resp) {
k<-ncol(resp)
p<-colMeans(resp,na.rm=TRUE)
q<-1-p
o<-rowSums(resp)
(k/(k-1))*(1-sum(p*q)/var(o))
}
@ben-domingue
ben-domingue / gist:a8ab9baed4f7c6b271e1f015c604af3b
Last active March 30, 2021 14:56
schoolgrowth simulation examples
library(schoolgrowth)
## > growthscores
## stuid school grade year subject Y
## 1 s0102 sch01 4 1 ela 50
## 2 s0196 sch01 4 1 ela 46
## 3 s0227 sch01 4 1 ela 5
## 4 s0284 sch01 4 1 ela 18
## 5 s0353 sch01 4 1 ela 15
simfun<-function(N,a1,beta.inter=.1) {
ff<-function(i,N,a1,a2,beta.inter) {
add.error<-function(t,alpha) {
ve<-var(t)/alpha-var(t)
e<-rnorm(N,mean=0,sd=sqrt(ve))
t+e
}
g<-rnorm(N)
e<-rnorm(N)
y<-.2*g+.5*e+beta.inter*g*e+rnorm(N)
@ben-domingue
ben-domingue / testing_xi.R
Created May 29, 2020 20:01
Test of Scaling in GxE
#this contains the funtion that does the ML estimation
ratio.test<-function(df,
hess=FALSE, #if FALSE, compute empirical hessian
ctl.list=list(maxit=1000,reltol=sqrt(.Machine$double.eps)/1000)
) {
##
anal_vcov <- function(pars, df){
neg_hess <- function(pars,df) { #computes the negative hessian
for (nm in names(pars)) assign(nm,pars[[nm]])
mu <- m*df$E + pi0*df$g + pi1*df$g*df$E
out<-list()
for (s1 in seq(0,.2,by=.01)) {
print(s1)
test<-numeric()
for (i in 1:250) {
N=1000
h=sqrt(.6)
e=sqrt(1-h^2)
m=.35
s0=sqrt(1-m^2)
@ben-domingue
ben-domingue / hierchical_model_example.R
Created March 19, 2020 12:14
Hierarchical model example
interplay<-function(x,std.time.in.item=FALSE,nspl=4,plot.den=TRUE,top.plot=TRUE,...) {
##x needs to have columns:
## item [item id]
## id [person id]
## diff [item difficulty]
## th [person theta]
## pv [irt-based p-value]
## rt [response time in metric you want to analyze]
##resp [item response]
#####################################################################
#get genetic risk score
read.table("/hrsshare/alz_context/pgs/alz_nc.profile",header=TRUE)->score
score[,c(2,6)]->score
names(score)<-c("subjectID","polygenic_score")
#rand fat files
load("/hrsshare/v2_hrs_linked.Rdata")
x[,c("subjectID","hhidpn","family","raracem","rahispan")]->ids
ids[ids$raracem=="1.white/caucasian" & ids$rahispan=="0. not hispanic",]->ids
merge(ids,score)->x
make_grs<-function(nm,df,fam.id="family",id="subjectID",
threshold.seq,
plink="/hrsshare/analytic_sample/hrs_geno_final",
resid.fm=NULL) {
dir.nm<-paste0("/tmp/",nm)
system(paste("mkdir",dir.nm))
getwd()->dir.save
setwd(dir.nm)
if (!is.null(resid.fm)) {
paste(nm,"~",resid.fm)->resid.fm