Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Distributions
using GLM
using RCall
using Random
Random.seed!(2)
n = 10000
ses = collect(1:n)
pgs = rand(Normal(), n)
low_ses = collect(1:Int(n*0.2))
medium_ses = collect(Int(n*0.2) + 1:Int(n*0.6))
high_ses = collect(Int(n*0.6) +1:n)
divergence_low_ses = rand(Normal(0, 4), length(low_ses))
divergence_middle_ses = rand(Normal(0, 2.6), length(medium_ses))
divergence_high_ses = rand(Normal(0, 2), length(high_ses))
divergence = vcat(divergence_low_ses, divergence_middle_ses, divergence_high_ses)
iq = pgs + divergence
scale(v) = (v .- mean(v)) / std(v)
iq_scaled = scale(iq)
ses_scaled = scale(ses)
show_cor(s, group) = println(s * ": ", cor(pgs[group], iq_scaled[group]))
show_cor("Overall correlation", ses)
show_cor("Low SES correlation", low_ses)
show_cor("Medium SES correlation", medium_ses)
show_cor("High SES correlation", high_ses)
iq_dot_pgs = iq_scaled .* pgs
@rput iq_dot_pgs
R"summary(lm(iq_dot_pgs ~ ses_scaled))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment