1 / 12

Programming and Simulations

Programming and Simulations. Frank Witmer 6 January 2011. Outline. General programming tips Programming loops Simulation Distributions Sampling Bootstrapping. General Programming Tips. Use meaningful variable names Include more comments than you think necessary Debugging your code

kass
Download Presentation

Programming and Simulations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming and Simulations Frank Witmer 6 January 2011

  2. Outline • General programming tips • Programming loops • Simulation • Distributions • Sampling • Bootstrapping

  3. General Programming Tips • Use meaningful variable names • Include more comments than you think necessary • Debugging your code • Since R is interpreted, non-function variables are available for inspection if execution terminates • Built-in debugging support: debug(), browser(), trace() • But generally adding print statements in functions is sufficient • Syntax highlighting! • http://sourceforge.net/projects/npptor/

  4. Loops • Because R is an interpreted language, all variables in the system are evaluated and stored at every step • So avoid loops for computationally intense analysis

  5. For & While loop syntax for (variable in sequence) { expression expression } while (condition) { expression expression }

  6. if/else control statements if ( condition1 ) { expression1 }else if ( condition2 ) { expression2 } else { expression3 }

  7. Ways to avoid loops (sometimes) • tapply:apply a function (FUN) to a variable based on a grouping variable • lapply:apply a function (FUN) to each variable in a given list • sapply:same as lapply but output is more user-friendly

  8. Data simulation • Can simulate data using standard distribution functions, e.g. core names norm, pois • Use ‘r’ prefix to generate random values of the distribution • rnorm(numVals, mean, sd) • rpois(numVals, mean) • Use set.seed() if you want your simulated data to be reproducible

  9. Standard distribution functions

  10. Sampling • Sample from a dataset using: sample(dataset, numItems, replace?) • Can use to simulate survey results or bootstrap statistical estimates

  11. Bootstrap overview • Method to measure accuracy of estimates from a sample empirically • For a sample of size n, draw many random samples, also of size n, with replacement • Two ways to bootstrap regression estimates • residual resampling: add resampled regression residuals to the original dep. var. & re-estimate • data resampling: sample complete cases of original data and estimate coefficients

  12. Recall: Boston Metadata

More Related