Skip to content

Instantly share code, notes, and snippets.

@kosugitti
Last active October 21, 2016 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosugitti/eebe103fefae22a907003c36d57dd174 to your computer and use it in GitHub Desktop.
Save kosugitti/eebe103fefae22a907003c36d57dd174 to your computer and use it in GitHub Desktop.
不倫報道間隔を予想する
## 2016年度の不倫報道データ
dataset <- as.data.frame(
matrix(c("ベッキー","2016-01-07",
"狩野英孝","2016-02-03",
"宮崎謙介議員","2016-02-10",
"桂文枝","2016-03-16",
"石井竜也","2016-03-17",
"乙武洋匡","2016-03-23",
"とにかく明るい安村","2016-04-01",
"NMB48木下春奈","2016-05-08",
"ファンキー加藤","2016-06-07",
"三遊亭円楽","2016-06-10",
"荻上チキ","2016-07-06",
"小倉優子の夫","2016-08-02",
"中村橋之助","2016-09-15",
"ジュビロ磐田の藤田義明","2016-10-04",
"浦沢直樹","2016-10-10",
"矢島悠子アナウンサー","2016-10-13",
"ラモス瑠偉","2016-10-18",
"小渕健太郎","2016-10-20"
),ncol=2,byrow=T))
names(dataset) <- c("who","date")
# POSIXct型に変換
furin <- as.POSIXct(dataset$date)
# 不倫報道が生じるまでの日数を計算
span <- c()
for(i in 1:(length(furin)-1)){
span[i] <- difftime(furin[i+1],furin[i])
}
# Stanで予測する
library(rstan)
options(mc.cores = parallel::detectCores())
# 「報道が生じる」のがポアソン分布,ポアソン分布で
# 次の観測が生じるまでの感覚は指数分布に従うと考えられる
stancode <- '
data{
int<lower=0> N;
real<lower=0> X[N];
}
parameters{
real<lower=0> lambda;
}
model{
target += exponential_lpdf(X|lambda);
}
generated quantities{
real<lower=0> mu;
mu = inv(lambda);
}
'
# 推定
datastan <- list(N=length(span),X=span)
model <- stan_model(model_code=stancode)
fit <- sampling(model,data=datastan)
print(fit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment