random-sample

how to generate random numbers with a specified lognormal distribution in R?

可紊 提交于 2020-01-29 08:01:16
问题 I would like to get 20 randomly generated numbers from a lognormal distribution with the geometric mean of 10 and geometric standard deviation of 2.5. Which R function should I use to accomplish this task? Thank you for your help! 回答1: The rlnorm function: rlnorm(20, log(10), log(2.5)) More generally distributions in R are generally available in d,p,q,r forms with those letters coming first followed by the distribution stem: norm , lnorm , unif , gamma , ... etc. Their help pages will contain

how to generate random numbers with a specified lognormal distribution in R?

落爺英雄遲暮 提交于 2020-01-29 08:01:06
问题 I would like to get 20 randomly generated numbers from a lognormal distribution with the geometric mean of 10 and geometric standard deviation of 2.5. Which R function should I use to accomplish this task? Thank you for your help! 回答1: The rlnorm function: rlnorm(20, log(10), log(2.5)) More generally distributions in R are generally available in d,p,q,r forms with those letters coming first followed by the distribution stem: norm , lnorm , unif , gamma , ... etc. Their help pages will contain

Making functions that set the random seed independent

半腔热情 提交于 2020-01-22 13:36:26
问题 Sometimes I want to write a randomized function that always returns the same output for a particular input. I've always implemented this by setting the random seed at the top of the function and then proceeding. Consider two functions defined in this way: sample.12 <- function(size) { set.seed(144) sample(1:2, size, replace=TRUE) } rand.prod <- function(x) { set.seed(144) runif(length(x)) * x } sample.12 returns a vector of the specified size randomly sampled from the set {1, 2} and rand.prod

Making functions that set the random seed independent

最后都变了- 提交于 2020-01-22 13:35:27
问题 Sometimes I want to write a randomized function that always returns the same output for a particular input. I've always implemented this by setting the random seed at the top of the function and then proceeding. Consider two functions defined in this way: sample.12 <- function(size) { set.seed(144) sample(1:2, size, replace=TRUE) } rand.prod <- function(x) { set.seed(144) runif(length(x)) * x } sample.12 returns a vector of the specified size randomly sampled from the set {1, 2} and rand.prod

R - random distribution with predefined min, max, mean, and sd values

北慕城南 提交于 2020-01-13 12:07:23
问题 I want to generate a random distribution of say 10,000 numbers with predefined min, max, mean, and sd values. I have followed this link setting upper and lower limits in rnorm to get random distribution with fixed min and max values. However, in doing so, mean value changes. For example, #Function to generate values between a lower limit and an upper limit. mysamp <- function(n, m, s, lwr, upr, nnorm) { set.seed(1) samp <- rnorm(nnorm, m, s) samp <- samp[samp >= lwr & samp <= upr] if (length

R - random distribution with predefined min, max, mean, and sd values

安稳与你 提交于 2020-01-13 12:04:15
问题 I want to generate a random distribution of say 10,000 numbers with predefined min, max, mean, and sd values. I have followed this link setting upper and lower limits in rnorm to get random distribution with fixed min and max values. However, in doing so, mean value changes. For example, #Function to generate values between a lower limit and an upper limit. mysamp <- function(n, m, s, lwr, upr, nnorm) { set.seed(1) samp <- rnorm(nnorm, m, s) samp <- samp[samp >= lwr & samp <= upr] if (length

R - random distribution with predefined min, max, mean, and sd values

梦想的初衷 提交于 2020-01-13 12:04:12
问题 I want to generate a random distribution of say 10,000 numbers with predefined min, max, mean, and sd values. I have followed this link setting upper and lower limits in rnorm to get random distribution with fixed min and max values. However, in doing so, mean value changes. For example, #Function to generate values between a lower limit and an upper limit. mysamp <- function(n, m, s, lwr, upr, nnorm) { set.seed(1) samp <- rnorm(nnorm, m, s) samp <- samp[samp >= lwr & samp <= upr] if (length

Difference between runif and sample in R?

梦想的初衷 提交于 2020-01-12 08:21:09
问题 In terms of probability distribution they use? I know that runif gives fractional numbers and sample gives whole numbers, but what I am interested in is if sample also use the 'uniform probability distribution'? 回答1: Consider the following code and output: > set.seed(1) > round(runif(10,1,100)) [1] 27 38 58 91 21 90 95 66 63 7 > set.seed(1) > sample(1:100, 10, replace=TRUE) [1] 27 38 58 91 21 90 95 67 63 7 This strongly suggests that when asked to do the same thing, the 2 functions give

How to create a stratified sample by state in R

拜拜、爱过 提交于 2020-01-12 05:56:53
问题 How can I create a stratified sample in R using the "sampling" package? My dataset has 355,000 observations. The code works fine up to the last line. Below is the code I wrote, but I always get the following message: "Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?" Please do not point me to older messages on Stackoverflow. I researched them, but have not been able to use them. Thank you. ## lpdata file has 355,000 observations # Exclude Puerto

How to create a stratified sample by state in R

故事扮演 提交于 2020-01-12 05:55:22
问题 How can I create a stratified sample in R using the "sampling" package? My dataset has 355,000 observations. The code works fine up to the last line. Below is the code I wrote, but I always get the following message: "Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?" Please do not point me to older messages on Stackoverflow. I researched them, but have not been able to use them. Thank you. ## lpdata file has 355,000 observations # Exclude Puerto