Last data update: 2014.03.03
|
R: A function to simplify simulations
pmgRepeatTrials | R Documentation |
A function to simplify simulations
Description
A simple function to repeat an expression several times as an aid to
simplifying simulations.
Usage
pmgRepeatTrials(expr, n = 10)
Arguments
expr |
An R expression, such as rnorm(1) or
{x <- rnorm(10); t.test(x)$p.value} that will be repeated n times.
|
n |
Number of times to repeat the expressions. The default is 10.
|
Details
This functions aids in doing simulations. Rather than explicitly write
a for loop or use sapply this function will call
sapply on the expression.
A GUI for this appears in pmg under the Simluation tab. The "quick
action" will call the function on the results of the simulation.
Value
The output of a sapply call can be a vector, matrix, ... If it
is a vector, it is transposed/
Note
This function and GUI was suggested by Daniel Kaplan at useR!2007
Author(s)
John Verzani
Examples
res <- pmgRepeatTrials(rnorm(1))
hist(res)
g = data.frame(
father = c(78.5, 78.5, 77.5, 76.0, 75.5),
mother = c(67.0, 68.0, 66.0, 65.5, 62.0),
sex = c("M", "M", "F", "F", "M"),
nkids = c(4, 4, 1, 2, 5)
)
res <- pmgRepeatTrials(coef(lm(father~ sex + sample(nkids),data=g)),100)
print(res)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(mcaGUI)
Loading required package: lattice
Loading required package: MASS
Loading required package: proto
Loading required package: foreign
Loading required package: gWidgets
Loading required package: gWidgetsRGtk2
Loading required package: RGtk2
Loading required package: cairoDevice
Loading required package: OTUbase
Loading required package: S4Vectors
Loading required package: stats4
Loading required package: BiocGenerics
Loading required package: parallel
Attaching package: 'BiocGenerics'
The following objects are masked from 'package:parallel':
clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
clusterExport, clusterMap, parApply, parCapply, parLapply,
parLapplyLB, parRapply, parSapply, parSapplyLB
The following objects are masked from 'package:stats':
IQR, mad, xtabs
The following objects are masked from 'package:base':
Filter, Find, Map, Position, Reduce, anyDuplicated, append,
as.data.frame, cbind, colnames, do.call, duplicated, eval, evalq,
get, grep, grepl, intersect, is.unsorted, lapply, lengths, mapply,
match, mget, order, paste, pmax, pmax.int, pmin, pmin.int, rank,
rbind, rownames, sapply, setdiff, sort, table, tapply, union,
unique, unsplit
Attaching package: 'S4Vectors'
The following objects are masked from 'package:base':
colMeans, colSums, expand.grid, rowMeans, rowSums
Loading required package: IRanges
Loading required package: ShortRead
Loading required package: BiocParallel
Loading required package: Biostrings
Loading required package: XVector
Loading required package: Rsamtools
Loading required package: GenomeInfoDb
Loading required package: GenomicRanges
Loading required package: GenomicAlignments
Loading required package: SummarizedExperiment
Loading required package: Biobase
Welcome to Bioconductor
Vignettes contain introductory material; view with
'browseVignettes()'. To cite Bioconductor, see
'citation("Biobase")', and for packages 'citation("pkgname")'.
Attaching package: 'ShortRead'
The following object is masked from 'package:gWidgetsRGtk2':
id
The following object is masked from 'package:gWidgets':
id
Loading required package: vegan
Loading required package: permute
This is vegan 2.4-0
================================================================================
Welcome to OTUbase version 1.22.0
Attaching package: 'OTUbase'
The following object is masked from 'package:GenomicAlignments':
seqnames
The following object is masked from 'package:SummarizedExperiment':
seqnames
The following object is masked from 'package:GenomicRanges':
seqnames
The following object is masked from 'package:GenomeInfoDb':
seqnames
Loading required package: bpca
Loading required package: scatterplot3d
Loading required package: rgl
Loading mcaGUI()
[1] "You can access the mcaGUI vignette by browsing to: "
[1] ""
GUI requires an interactive environment
> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/mcaGUI/pmgRepeatTrials.Rd_%03d_medium.png", width=480, height=480)
> ### Name: pmgRepeatTrials
> ### Title: A function to simplify simulations
> ### Aliases: pmgRepeatTrials
> ### Keywords: datagen
>
> ### ** Examples
>
> res <- pmgRepeatTrials(rnorm(1))
> hist(res)
>
> g = data.frame(
+ father = c(78.5, 78.5, 77.5, 76.0, 75.5),
+ mother = c(67.0, 68.0, 66.0, 65.5, 62.0),
+ sex = c("M", "M", "F", "F", "M"),
+ nkids = c(4, 4, 1, 2, 5)
+ )
> res <- pmgRepeatTrials(coef(lm(father~ sex + sample(nkids),data=g)),100)
> print(res)
(Intercept) sexM sample(nkids)
[1,] 72.61290 2.7419355 0.91935484
[2,] 78.35714 3.7857143 -1.07142857
[3,] 74.35937 0.4843750 0.79687500
[4,] 72.61290 2.7419355 0.91935484
[5,] 76.89062 0.7656250 -0.04687500
[6,] 76.46875 0.7187500 0.09375000
[7,] 74.35937 0.4843750 0.79687500
[8,] 79.98077 -0.3269231 -0.80769231
[9,] 77.22727 0.9727273 -0.19090909
[10,] 77.22727 0.9727273 -0.19090909
[11,] 74.44231 1.5192308 0.57692308
[12,] 78.57812 0.9531250 -0.60937500
[13,] 75.22581 1.4838710 0.33870968
[14,] 76.46875 0.7187500 0.09375000
[15,] 75.22581 1.4838710 0.33870968
[16,] 76.89062 0.7656250 -0.04687500
[17,] 76.00000 0.4000000 0.30000000
[18,] 77.73438 0.8593750 -0.32812500
[19,] 74.00000 1.1428571 0.78571429
[20,] 77.22727 0.9727273 -0.19090909
[21,] 77.73437 0.8593750 -0.32812500
[22,] 76.46875 0.7187500 0.09375000
[23,] 81.75806 -1.6612903 -1.11290323
[24,] 76.40909 0.5909091 0.13636364
[25,] 73.91935 2.1129032 0.62903226
[26,] 76.42857 0.1428571 0.21428571
[27,] 78.45455 1.5454545 -0.68181818
[28,] 74.00000 1.1428571 0.78571429
[29,] 74.77273 -0.1727273 0.79090909
[30,] 74.44231 1.5192308 0.57692308
[31,] 77.22727 0.9727273 -0.19090909
[32,] 78.45455 1.5454545 -0.68181818
[33,] 75.82692 1.0576923 0.23076923
[34,] 73.91935 2.1129032 0.62903226
[35,] 76.53226 0.8548387 0.04838710
[36,] 77.73438 0.8593750 -0.32812500
[37,] 76.00000 0.4000000 0.30000000
[38,] 81.75806 -1.6612903 -1.11290323
[39,] 78.45455 1.5454545 -0.68181818
[40,] 72.61290 2.7419355 0.91935484
[41,] 76.53226 0.8548387 0.04838710
[42,] 81.75806 -1.6612903 -1.11290323
[43,] 75.82692 1.0576923 0.23076923
[44,] 77.73437 0.8593750 -0.32812500
[45,] 80.28571 7.4285714 -2.35714286
[46,] 75.20312 0.5781250 0.51562500
[47,] 78.57812 0.9531250 -0.60937500
[48,] 78.15625 0.9062500 -0.46875000
[49,] 74.77273 -0.1727273 0.79090909
[50,] 76.42857 0.1428571 0.21428571
[51,] 77.73437 0.8593750 -0.32812500
[52,] 75.22581 1.4838710 0.33870968
[53,] 78.45455 1.5454545 -0.68181818
[54,] 76.42857 0.1428571 0.21428571
[55,] 74.35937 0.4843750 0.79687500
[56,] 73.91935 2.1129032 0.62903226
[57,] 74.50000 -3.5000000 1.50000000
[58,] 75.20312 0.5781250 0.51562500
[59,] 78.45455 1.5454545 -0.68181818
[60,] 76.00000 0.4000000 0.30000000
[61,] 81.75806 -1.6612903 -1.11290323
[62,] 76.53226 0.8548387 0.04838710
[63,] 76.00000 0.4000000 0.30000000
[64,] 74.35937 0.4843750 0.79687500
[65,] 78.57812 0.9531250 -0.60937500
[66,] 76.00000 0.4000000 0.30000000
[67,] 76.00000 0.4000000 0.30000000
[68,] 78.35714 3.7857143 -1.07142857
[69,] 72.61290 2.7419355 0.91935484
[70,] 74.44231 1.5192308 0.57692308
[71,] 77.73438 0.8593750 -0.32812500
[72,] 73.91935 2.1129032 0.62903226
[73,] 74.50000 -3.5000000 1.50000000
[74,] 74.35937 0.4843750 0.79687500
[75,] 75.22581 1.4838710 0.33870968
[76,] 77.63636 1.1636364 -0.35454545
[77,] 75.50000 0.9285714 0.35714286
[78,] 78.57812 0.9531250 -0.60937500
[79,] 75.82692 1.0576923 0.23076923
[80,] 76.46875 0.7187500 0.09375000
[81,] 77.00000 0.7142857 -0.07142857
[82,] 76.53226 0.8548387 0.04838710
[83,] 77.73437 0.8593750 -0.32812500
[84,] 75.50000 0.9285714 0.35714286
[85,] 76.53226 0.8548387 0.04838710
[86,] 75.82692 1.0576923 0.23076923
[87,] 81.75806 -1.6612903 -1.11290323
[88,] 73.91935 2.1129032 0.62903226
[89,] 77.73437 0.8593750 -0.32812500
[90,] 74.35937 0.4843750 0.79687500
[91,] 74.50000 -3.5000000 1.50000000
[92,] 75.50000 0.9285714 0.35714286
[93,] 81.75806 -1.6612903 -1.11290323
[94,] 77.00000 0.7142857 -0.07142857
[95,] 76.42857 0.1428571 0.21428571
[96,] 78.45455 1.5454545 -0.68181818
[97,] 76.40909 0.5909091 0.13636364
[98,] 74.78125 0.5312500 0.65625000
[99,] 77.00000 0.7142857 -0.07142857
[100,] 78.45455 1.5454545 -0.68181818
>
>
>
>
>
> dev.off()
null device
1
>
|
|