Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Courtesy of some generous advice from my colleagues, I now have one function that creates simulated data given a specified number of groups, a set of contrasts, a set of regression coefficients, a specified N per cell, and a specified within-group variance</p> <pre><code>sim.factor &lt;- function(levels, contr, beta, perCell, errorVar){ # Build design matrix X X &lt;- cbind(rep(1,levels*perCell), kronecker(contr, rep(1,perCell))) # Generate y y &lt;- X %*% beta + rnorm(levels*perCell, sd=sqrt(errorVar)) # Build and return data frame dat &lt;- cbind.data.frame(y, X[,-1]) names(dat)[-1] &lt;- colnames(contr) return(dat) } </code></pre> <p>I also wrote a function that, given a set of regression coefficients, N per cell, number of groups, set of orthogonal contrasts, desired delta-R^2 for a contrast of interest, returns the required within-group variance:</p> <pre><code>ws.var &lt;- function(levels, contr, beta, perCell, dc){ # Build design matrix X X &lt;- cbind(rep(1,levels), contr) # Generate the expected means means &lt;- X %*% beta # Find the sum of squares due to each contrast var &lt;- (t(means) %*% contr)^2 / apply(contr^2 / perCell, 2, sum) # Calculate the within-conditions sum of squares wvar &lt;- var[1] / dc - sum(var) # Convert the sum of squares to variance errorVar &lt;- wvar / (3 * (perCell - 1)) return(errorVar) } </code></pre> <p>After doing some testing as follows, the functions seem to generate the desired delta R^2 for contrast c1.</p> <pre><code>contr &lt;- contr.helmert(3) colnames(contr) &lt;- c("c1","c2") beta &lt;- c(0, 1, 0) perCell &lt;- 50 levels = 3 dc &lt;- .08 N &lt;- 1000 # Calculate the error variance errorVar &lt;- ws.var(levels, contr, beta, perCell, dc) # To store delta R^2 values d1 &lt;- vector("numeric", length = N) # Use the functions for(i in 1:N) { d &lt;- sim.factor(levels=3, contr=contr, beta=beta, perCell=perCell, errorVar=errorVar) d1[i] &lt;- lm.sumSquares(lm(y ~ c1 + c2, data = d))[1, 2] # From the lmSupport package } m &lt;- round(mean(d1), digits = 3) bmp("Testing simulation functions.bmp") hist(d1, xlab = "Percentage of variance due to c1", main = "") text(.18, 180, labels = paste("Mean =", m)) dev.off() </code></pre> <p>Patrick</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload