Transcription of Simulating Data in R - xcelab.net
1 Simulating data in RGeneral StrategyThe basic approach to Simulating data making fake data is to (1) take the statistical model,(2) pick some parameter values, and (3) use the random form of the model s probability distribution,with the model embedded inside it, to generate random samples. So for a modelyi Zink( + xi, ), data can be simulated from the model using:a <- 3b <- <- 12y <- rzink( n , a + b*x , g )wherea,b, andgare the parameters,xis a vector of (possibly fake) prediction values, andnisequal to the length ofx, the number ofyvalues to ExampleSuppose we have a Gaussian regression model,yi N( + 1xi+ 2zi, ). You know the built-indensity function for Gaussian values isdnorm. The corresponding function to generate randomGaussian values isrnorm this is almost always the case, that the random function is named afterthe density function, by changing the initial d to a r.
2 To simulate:a <- <- <- -5s <- 3y <- rnorm( length(x) , mean=a + b1*x + b2*z , sd=s )The vectorywill hold randomly sampled Gaussian DistributionsDistributionFunctionExample Normalrnorm y <- rnorm(length(x),mean=a+b*x,sd=s)Binomial rbinom y <- rbinom(length(x),prob=logit(a+b*x),size= n)Poissonrpois y <- rpois(length(x),lambda=a+b*x)Gammargamma y <- rgamma(length(x),shape=a+b*x,scale=th)Ne gative binomialrnbinom y <- rnbinom(length(x),mu=a+b*x,size=n) or rgamma/rpois y1 <- rgamma(length(x),shape=a+b*x,scale=th)y2 <- rpois(length(y1),lambda=y1)Exponentialre xp y <- rexp(length(x),rate=a+b*x)Beta-binomialr beta/rbinom y1 <- rbeta(length(x),shape1=a+b*x,shape2=s2)
3 Y2 <- rbinom(length(y1),prob=y1,size=n)1