Skip to content

Instantly share code, notes, and snippets.

View tbates's full-sized avatar
🙈

Tim Bates tbates

🙈
View GitHub Profile
@tbates
tbates / coin.flip.R
Created September 27, 2012 21:51
Turned coins
n = 1000;
coins = rep(0, n)
for (i in 2:n) {
whatToFlip = seq(from = i, to = n, by = i)
coins[whatToFlip] = coins[whatToFlip] + 1
}
sqrt(which(!as.logical(coins%%2)))
# [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
@tbates
tbates / lavaan_version.R
Created October 2, 2012 15:09
SEM model in lavaan
require(lavaan)
bifactor <- "
general.factor =~ Easy_Reservation + Preferred_Seats + Flight_Options + Ticket_Prices
+ Seat_Comfort + Seat_Roominess + Overhead_Storage
+ Clean_Aircraft + Courtesy + Friendliness + Helpfulness + Service
ticketing =~ Easy_Reservation + Preferred_Seats + Flight_Options + Ticket_Prices
aircraft =~ Seat_Comfort + Seat_Roominess + Overhead_Storage + Clean_Aircraft
service =~ Courtesy + Friendliness + Helpfulness + Service"
@tbates
tbates / p.R
Created October 14, 2012 20:54
distribution of p-values at high power
library(MASS)
library(ggplot2)
# move n around to alter sample size
# move r around to alter effect size
n = 1000; r = .5
desiredCovMatrix = matrix(c(1,r,r, 1) ,nrow=2, ncol=2);
count = 1000 # number of replications
out = rep(NA,count) # array to store the results
for (i in 1:count) {
@tbates
tbates / searchlink.rb
Created October 19, 2012 09:55 — forked from ttscoff/searchlink.rb
SearchLink creates Markdown links from automatic searches based on special syntax.
umxSaturated <- function(m1, evaluate = T) {
# Use case
# m1_sat = umxSaturated(m1)
# summary(m1, SaturatedLikelihood=m1_sat$SaturatedLikelihood, IndependenceLikelihood=m1_sat$IndependenceLikelihood)
manifests = m1@manifestVars
nVar = length(manifests)
theData = m1@data@observed
dataMeans = colMeans(theData)
meansLabels = paste("mean", 1:nVar, sep="")
loadingsLabels = paste("F", 1:nVar, "loading", sep="")
setMethod("imxVerifyModel", "MxRAMModel",
function(model) {
if ((length(model$A) == 0) ||
(length(model$S) == 0) ||
(length(model$F) == 0)) {
msg <- paste("The RAM model", omxQuotes(model@name),
"does not contain any paths.",
" Are you just starting out? you need to add paths like",
if (identical(options[["Standard Errors"]], "Yes") &&
identical(options[["Calculate Hessian"]], "No")) {
msg <- paste('The "Standard Errors" option is enabled and',
'the "Calculate Hessian" option is disabled. Generating',
'standard errors requires the Hessian calculation. Please',
'disable standard errors or enable the Hessian calculation.\n',
'You can do this with\n',
'model <- mxOption(model, "Standard Errors", "No")\n',
'or\n',
'model <- mxOption(model, "Calculate Hessian", "Yes")'
@tbates
tbates / age.R
Created November 20, 2012 13:11
persons = rnorm(100, 20,10)
nYearsToRun = 500
births = deaths = pop = rep(NA,nYearsToRun)
for (y in 1:nYearsToRun) {
populationSize = length(persons)
persons = persons+1 # age everybody
# death
persons <- persons[persons < rnorm(populationSize, 77, 15)]
# Above are some results from the a matrix of a
# trivariate cholesky with the above diag cells filled in
A = matrix(nrow = 3, byrow = T, c(.71, -.28, .15, -.28, .61, -.38, .15, -.38, .000001))
[,1] [,2] [,3]
[1,] 0.71 -0.28 1.5e-01
[2,] -0.28 0.61 -3.8e-01
[3,] 0.15 -0.38 1.0e-06
# Yeah... so that's wrong. that's the lower a matrix.
@tbates
tbates / factor.R
Last active December 15, 2015 08:09
How does factor analysis work? eigenvalues and eigenvectors
x <- data.frame(matrix(ncol = 2, byrow = T, c(
-0.7, 0.2,
2.1, 2.7,
1.7, 2.3,
1.4, 0.8,
1.9, 2.0,
1.8, 1.0,
0.4, -0.4,
1.1, 0.3,
0.9, 0.4,