我是新手,需要帮助。 我正在为我的论文制作一个基于代理的模型。 我现在所困扰的是我的人口中的生与死。 我有一个data.frame与男性,女性,年龄,preg,不preg和活着或死。 我需要做的是 * for n for people for FOR FOR EACH PERSON
使用随机数决定性别(例如,< 0.5 =男性)
决定年龄,画一个随机数0到80
将Alive设置为TRUE
PREGO-男性和生殖前或生殖后女性,设为假
对于育龄女性,掷骰子
我也试图将所有这些数据存储在数据框中,以便为模拟创建一个开始的poplulation。
就像我说的那样,我只是盯着编码而且我在语法等方面受到了限制 任何帮助甚至可能有用的资源都非常感谢
答案 0 :(得分:2)
我的回答,没有循环:
#choose sample size and initialize dataframe with sex and age
samplesize <- 5000
df <- data.frame(sex = round(runif(samplesize)),
age = round(runif(samplesize, 0, 80)))
# all are alive and pregnant
df$alive <- TRUE
df$prego <- TRUE
# men and old/young women are not pregnant
df$prego[(df$sex == 1) | (df$age > 60) | (df$age < 14)] <- FALSE
# only some of the potentially pregnant women are pregnant
df$prego[df$prego == TRUE] <- sample(c(T,F), sum(df$prego), replace = T)
# replace 1s and 0s with M and F
df$sex[df$sex == 1] <- "M"
df$sex[df$sex == 0] <- "F"
答案 1 :(得分:0)
这就是我要做的事情:
library(data.table)
#starting variables
probMale <- .5
ageMin <- 0
ageMax <- 80
preProductiveAge <- 15
postProductiveAge <- 44
probPregnant <- .3
sampleSize <- 10000
listOut <- list()
for(i in 1:sampleSize){
sex <- sample(c('male', 'female'), size = 1, prob = c(probMale, 1-probMale))
age <- sample(0:80, size = 1)
alive <- TRUE
if(sex == 'male'){
prego <- FALSE
} else if(age >= postProductiveAge){
prego <- FALSE
} else if(age <= preProductiveAge){
prego <- FALSE
} else{
prego <- sample(c(TRUE, FALSE), size = 1, prob = c(probPregnant, 1-probPregnant))
}
listOut[[i]] <- data.frame(sex = sex, age = age, alive = alive, prego = prego)
}
df <- rbindlist(listOut)
我保留了在循环外定义的变量,因此您可以轻松修改。
答案 2 :(得分:0)
无条件变量最初在tibble()
内定义。条件变量在随后的dplyr::mutate()
子句中设置。
library(magrittr)
person_count <- 20
range_fertile <- c(20, 45)
possible_genders <- c("male", "female")
possible_ages <- 1:80
pregnant_probability <- .14
tibble::tibble(
gender = sample(possible_genders , person_count, replace=T),
age = sample(possible_ages , person_count, replace=T),
alive = TRUE
) %>%
dplyr::mutate(
is_fertile_age = (gender=="female") & (range_fertile[1] <= age & age <= range_fertile[2]),
is_pregnant = (is_fertile_age & sample(c(T,F), person_count, prob = c(pregnant_probability, 1-pregnant_probability), replace=T))
)