簡體   English   中英

如何為運行示例生成帶有 NA 值的月度時間序列數據?

[英]How to generate monthly time series data with NA values for running examples?

對於這個問題的簡單性,我提前道歉。

如何使用 set seed 之類的東西生成每月時間序列數據集? 我對來自兩個包的結果有疑問,但需要創建一個示例數據集作為示例顯示。 我的數據集需要在其中包含一些 NA 值。

問候,

西蒙

這是 1000 個日期的隨機列表 +- 從今天起 5 年,使用simstudy包缺少一些數據(請提供示例數據和預期輸出以獲得更具體的答案):

library(simstudy)
library(dplyr)
library(lubridate)

set.seed(1724)

# define data
def <- defData(varname = "tmp", dist = "uniform", formula = "0;1") # sumstudy seems to crash when adding missing data with only 1 column
def <- defData(def, varname = "date", dist = "uniform", formula = "-5;5") # +- 5 years
df_full <- genData(1000, def)

##### missing data ----
defM <- defMiss(varname = "date", formula = 0.1, logit.link = F)
df_missing <- genMiss(df_full, defM, idvars = "id")

# Create data with missing values
df <- genObs(df_full, df_missing, idvars = "id")

df %>%
  as_tibble() %>%
  select(-tmp) %>%
  mutate(date = ymd(floor_date(as.POSIXct(Sys.Date()) + date * 365 * 24 * 60 * 60, unit = "day")), # +- 5 years from today
         month = format(date, "%Y-%m"))

# A tibble: 1,000 x 3
      id date       month  
   <int> <date>     <chr>  
 1     1 NA         NA     
 2     2 2021-09-12 2021-09
 3     3 2023-11-08 2023-11
 4     4 2015-03-02 2015-03
 5     5 2021-08-12 2021-08
 6     6 2021-10-20 2021-10
 7     7 2017-05-17 2017-05
 8     8 2019-04-12 2019-04
 9     9 NA         NA     
10    10 NA         NA     
# ... with 990 more rows

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM