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 9319129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:18:22+00:00 2026-06-19T03:18:22+00:00

Is there a single function, similar to runif, rnorm and the like which will

  • 0

Is there a single function, similar to “runif”, “rnorm” and the like which will produce simulated predictions for a linear model? I can code it on my own, but the code is ugly and I assume that this is something someone has done before.

slope = 1.5
intercept = 0
x = as.numeric(1:10)
e = rnorm(10, mean=0, sd = 1)
y = slope * x + intercept + e
fit = lm(y ~ x, data = df)
newX = data.frame(x = as.numeric(11:15))

What I’m interested in is a function that looks like the line below:

sims = rlm(1000, fit, newX)

That function would return 1000 simulations of y values, based on the new x variables.

  • 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-06-19T03:18:23+00:00Added an answer on June 19, 2026 at 3:18 am

    Showing that Gavin Simpson’s suggestion of modifying stats:::simulate.lm is a viable one.

    ## Modify stats:::simulate.lm by inserting some tracing code immediately
    ## following the line that reads "ftd <- fitted(object)" 
    trace(what = stats:::simulate.lm,
          tracer = quote(ftd <- list(...)[["XX"]]),
          at = list(6))
    
    ## Prepare the data and 'fit' object 
    df <- data.frame(x =x<-1:10, y = 1.5*x + rnorm(length(x)))
    fit <- lm(y ~ x, data = df)
    
    ## Define new covariate values and compute their predicted/fitted values
    newX <- 8:1
    newFitted <- predict(fit, newdata = data.frame(x = newX))
    
    ## Pass in fitted via the argument 'XX'
    simulate(fit, nsim = 4, XX = newFitted)
    #        sim_1     sim_2       sim_3     sim_4
    # 1 11.0910257 11.018211 10.95988582 13.398902
    # 2 12.3802903 10.589807 10.54324607 11.728212
    # 3  8.0546746  9.925670  8.14115433  9.039556
    # 4  6.4511230  8.136040  7.59675948  7.892622
    # 5  6.2333459  3.131931  5.63671024  7.645412
    # 6  3.7449859  4.686575  3.45079655  5.324567
    # 7  2.9204519  3.417646  2.05988078  4.453807
    # 8 -0.5781599 -1.799643 -0.06848592  0.926204
    

    That works, but this is a cleaner (and likely better) approach:

    ## A function for simulating at new x-values
    simulateX <- function(object, nsim = 1, seed = NULL, X, ...) {
        object$fitted.values <- predict(object, X)
        simulate(object = object, nsim = nsim, seed = seed, ...)
    }
        
    ## Prepare example data and a fit object
    df <- data.frame(x =x<-1:10, y = 1.5*x + rnorm(length(x)))
    fit <- lm(y ~ x, data = df)
    
    ## Supply new x-values in a data.frame of the form expected by
    ## the newdata= argument of predict.lm()
    newX <- data.frame(x = 8:1)
       
    ## Try it out
    simulateX(fit,  nsim = 4, X = newX)
    #       sim_1     sim_2     sim_3     sim_4
    # 1 11.485024 11.901787 10.483908 10.818793
    # 2 10.990132 11.053870  9.181760 10.599413
    # 3  7.899568  9.495389 10.097445  8.544523
    # 4  8.259909  7.195572  6.882878  7.580064
    # 5  5.542428  6.574177  4.986223  6.289376
    # 6  5.622131  6.341748  4.929637  4.545572
    # 7  3.277023  2.868446  4.119017  2.609147
    # 8  1.296182  1.607852  1.999305  2.598428
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a PHP string function that transforms a multi-line string into a single-line
Is there a function, method, or language construction allowing to retrieve a single column
Simply Asking, Is there any function available in mysql to split single row elements
I know there are functions to like is_single() that will return data about the
Is there a single preprocessor token that can be used to detect any iPhone
Is there a single query (subqueries in it are allowed) where I can copy
Is there a syntax for documenting functions which take a single configuration array, rather
TL;DR Summary: I need a single command-line application which I can use to get
I've read every post on stack on similar topics which there is many, but
There are some related questions about unpacking single-value tuples, but I'd like to know

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.