Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7430749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:11:24+00:00 2026-05-29T09:11:24+00:00

Let’s say that you have a normally distributed variable y with a 3-group categorical

  • 0

Let’s say that you have a normally distributed variable y with a 3-group categorical predictor x that has the orthogonal contrasts c1 and c2. I am trying to create a program in R that, given x, c1, and c2, creates y such that c1 and c2 have effect sizes r1 and r2 specified by the user.

For example, let’s say that x, c1, c2, r1, and r2 were created like the following:

x <- factor(rep(c(1, 2, 3), 100))
contrasts(x) <- matrix(c(0, -.5, .5, -2/3, 1/3, 1/3), 
  nrow = 3, ncol = 2, dimnames = list(c("1", "2", "3"), c("c1", "c2")))

contrasts(x)
    c1         c2
1  0.0 -0.6666667
2 -0.5  0.3333333
3  0.5  0.3333333

r1 <- .09
r2 <- 0

I would like the program to create y such that the variance in y accounted for by c1 equals r1 (.09) and the variance in y accounted for by c2 equals r2 (0).

Does anybody know how I might go about this? I know that I should be using the rnorm function, but I’m stuck on which population means / sds rnorm should use when it does its sampling.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:11:25+00:00Added an answer on May 29, 2026 at 9:11 am

    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

    sim.factor <- function(levels, contr, beta, perCell, errorVar){
      # Build design matrix X
      X <- cbind(rep(1,levels*perCell), kronecker(contr, rep(1,perCell)))
      # Generate y
      y <- X %*% beta + rnorm(levels*perCell, sd=sqrt(errorVar))
      # Build and return data frame
      dat <- cbind.data.frame(y, X[,-1])
      names(dat)[-1] <- colnames(contr)
      return(dat)
    }
    

    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:

    ws.var <- function(levels, contr, beta, perCell, dc){
      # Build design matrix X
      X <- cbind(rep(1,levels), contr)
      # Generate the expected means
      means <- X %*% beta
      # Find the sum of squares due to each contrast 
      var <- (t(means) %*% contr)^2 / apply(contr^2 / perCell, 2, sum)
      # Calculate the within-conditions sum of squares
      wvar <- var[1] / dc - sum(var)
      # Convert the sum of squares to variance
      errorVar <- wvar / (3 * (perCell - 1))
      return(errorVar)
    }
    

    After doing some testing as follows, the functions seem to generate the desired delta R^2 for contrast c1.

    contr <- contr.helmert(3)
    colnames(contr) <- c("c1","c2")
    beta <- c(0, 1, 0)
    perCell <- 50
    levels = 3
    dc <- .08
    N <- 1000
    
    # Calculate the error variance
    errorVar <- ws.var(levels, contr, beta, perCell, dc)
    
    # To store delta R^2 values
    d1 <- vector("numeric", length = N)
    
    # Use the functions
    for(i in 1:N)
    {
       d <- sim.factor(levels=3,
                       contr=contr,
                       beta=beta,
                       perCell=perCell,
                       errorVar=errorVar)
       d1[i] <- lm.sumSquares(lm(y ~ c1 + c2, data = d))[1, 2] # From the lmSupport package
    }
    
    m <- 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()
    

    Patrick

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have some text as follows: do this, do that, then this,
Let's say an Owner has a collection of Watch(es). I am trying to create
Let's say that I have a set of relations that looks like this: relations
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
let's say that I have string: s = Tuple: and Tuple (stored in a
Let's say for example, I have the following javascript function that returns a boolean:
Let's say I'm building a data access layer for an application. Typically I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.