simstudy:genData产生相同的值

时间:2018-12-14 15:53:16

标签: r package simulation

这是我第一次尝试模拟数据-我们想模拟一个数据集,并选择使用以下代码来使用simstudy:

def <- defData(varname='median_household_income',formula=mean(
               df$median_household_income))
def <- defData(def, varname='share_unemployed_seasonal',formula=mean(
               df$share_unemployed_seasonal))
def <- defData(def, varname='share_population_in_metro_areas',
               formula=mean(df$share_population_in_metro_areas))
def <- defData(def, varname='share_population_with_high_school_degree',
               formula=mean(df$share_population_with_high_school_degree))
def <- defData(def, varname='share_non_citizen',
               formula=mean(df$share_non_citizen))
def <- defData(def, varname='share_white_poverty',
               formula=mean(df$share_white_poverty))
def <- defData(def, varname='gini_index',formula=mean(df$gini_index))
def <- defData(def, varname='share_non_white',formula=mean(df$share_non_white))
def <- defData(def, varname='share_voters_voted_trump',
               formula=mean(df$share_voters_voted_trump))
#outcome
def <- defData(def, varname='avg_hatecrimes_per_100k_fbi',formula=
               ".0001*median_household_income + 44*share_unemployed_seasonal + 
               -2.8*share_population_in_metro_areas +
               24*share_population_with_high_school_degree + 22*share_non_citizen + 
               3.2*share_white_poverty + 55*gini_index + -4*share_non_white + 
               -2.6*share_voters_voted_trump")

#generate simulated data
df_sim <- genData(10000,def)

输出看起来像这样:

 head(df_sim)
 id median_household_income share_unemployed_seasonal share_population_in_metro_areas
1:  1                55223.61                0.04956863                       0.7501961
2:  2                55223.61                0.04956863                       0.7501961
3:  3                55223.61                0.04956863                       0.7501961
4:  4                55223.61                0.04956863                       0.7501961
5:  5                55223.61                0.04956863                       0.7501961
6:  6                55223.61                0.04956863                       0.7501961

为什么所有生成的值都相同?我的理解是,默认情况下变量是根据正态分布生成的。感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:1)

我发现您是指软件包simstudy。如果您查看defData函数(link here)的文档,则会发现variance函数有defData参数,默认为零。如果您希望获得不同的观察结果,则需要将此值设置为大于0的数字。

defData函数的默认行为:

defData(dtDefs = NULL, varname, formula, variance = 0,
  dist = "normal", link = "identity", id = "id")

所以您可能想运行类似的命令

def <- defData(varname='median_household_income',
               formula=mean(df$median_household_income),
               variance = 1)