(kabuto <- tibble(noudo = c(1.69072, 1.7242, 1.7552, 1.7842, 1.8113, 1.8369, 1.8610, 1.8839),
all = c(59, 60, 62, 56, 63, 59, 62, 60),
death = c(6, 13, 18, 28, 52, 53, 61, 60)))
## # A tibble: 8 x 3
## noudo all death
## <dbl> <dbl> <dbl>
## 1 1.69 59 6
## 2 1.72 60 13
## 3 1.76 62 18
## 4 1.78 56 28
## 5 1.81 63 52
## 6 1.84 59 53
## 7 1.86 62 61
## 8 1.88 60 60
kabuto %>% mutate(death_ratio = death/all*100) %>%
ggplot(aes(x = noudo, y = death_ratio)) + geom_point() + theme_base(base_family = "HiraKakuPro-W3") +
labs(x = "二硫化炭酸ガスの濃度", y = "カブトムシの死亡率(%)") + xlim(1.65, 1.9) + ylim(10, 100)
考えてみたけどうまくいかないです。理由はわかっているので、今後アップデートしてきます。
logistic <- function(x, b1, b2){
N <- length(x)
pi <- rep(0, length.out = N)
for(n in 1:N){
pi[n] <- exp(b1+(b2*x[n]))/(1+exp(b1+(b2*x[n])))
}
return(pi)
}
XWX <- function(data, b1, b2){
element_11 <- sum(data$all*logistic(data$noudo, b1, b2)*(1-logistic(data$noudo, b1, b2)))
element_12 <- sum(data$all*logistic(data$noudo, b1, b2)*data$noudo*(1-logistic(data$noudo, b1, b2)))
element_21 <- sum(data$all*logistic(data$noudo, b1, b2)*data$noudo*(1-logistic(data$noudo, b1, b2)))
element_22 <- sum(data$all*logistic(data$noudo, b1, b2)*data$noudo*data$noudo*(1-logistic(data$noudo, b1, b2)))
xwx <- matrix(c(element_11, element_12, element_21, element_22), ncol = 2, byrow = TRUE)
return(xwx)
}
XWz <- function(data, b1, b2){
upper <- sum(data$death*data$all*logistic(data$noudo, b1, b2)*(1-logistic(data$noudo, b1, b2)))
lower <- sum(data$death*data$all*logistic(data$noudo, b1, b2)*data$noudo*(1-logistic(data$noudo, b1, b2)))
xwz <- matrix(c(upper, lower), ncol = 1)
return(xwz)
}
b_new <- function(mat1, mat2){
b_new = solve(mat1) %*% mat2
return(b_new)
}
# b_newを収束するまで反復計算
# 初期値を与える
b1 <- 0
b2 <- 0
N <- 1000 # 適当な数字(反復計算の最大回数)
# 反復計算
for(i in 1:N){
mat1 <- XWX(kabuto, b1 = b1, b2 = b2)
mat2 <- XWz(kabuto, b1 = b1, b2 = b2)
bnew <- b_new(mat1, mat2)
if(abs(b1-bnew[1,]) <= 0.00001 & abs(b2-bnew[2,]) <= 0.00001) break
b1 <- bnew[1, ]
b2 <- bnew[2, ]
}
# 結果を見てみる
bnew
## [,1]
## [1,] -347.2693
## [2,] 208.9461
# GLMでやってみとく
glm(cbind(death, (all-death)) ~ noudo, data = kabuto, family = binomial) %>% summary()
##
## Call:
## glm(formula = cbind(death, (all - death)) ~ noudo, family = binomial,
## data = kabuto)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.5940 -0.3945 0.8331 1.2589 1.5938
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -60.721 5.181 -11.72 <2e-16 ***
## noudo 34.272 2.912 11.77 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 284.202 on 7 degrees of freedom
## Residual deviance: 11.229 on 6 degrees of freedom
## AIC: 41.427
##
## Number of Fisher Scoring iterations: 4
(asagao <- tibble(treatment = c("cont", "cont", "treat", "treat"),
type = c("futei", "yaku", "futei", "yaku"),
"40" = c(55, 102, 55, 76),
"150" = c(52, 99, 50, 81),
"350" = c(57, 108, 50, 90)) %>% gather(key = power, value = n, -treatment, -type) %>%
mutate_at(vars(power), funs(as.numeric(.))))
## Warning: funs() is soft deprecated as of dplyr 0.8.0
## Please use a list of either functions or lambdas:
##
## # Simple named list:
## list(mean = mean, median = median)
##
## # Auto named with `tibble::lst()`:
## tibble::lst(mean, median)
##
## # Using lambdas
## list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## This warning is displayed once per session.
## # A tibble: 12 x 4
## treatment type power n
## <chr> <chr> <dbl> <dbl>
## 1 cont futei 40 55
## 2 cont yaku 40 102
## 3 treat futei 40 55
## 4 treat yaku 40 76
## 5 cont futei 150 52
## 6 cont yaku 150 99
## 7 treat futei 150 50
## 8 treat yaku 150 81
## 9 cont futei 350 57
## 10 cont yaku 350 108
## 11 treat futei 350 50
## 12 treat yaku 350 90
asagao %>% spread(key = type, value = n) %>% mutate(ratio = futei/yaku) %>%
ggplot(aes(x = log(power), y = ratio)) + geom_point(aes(color = treatment)) +
theme_base(base_family = "HiraKakuPro-W3") + labs(x = "Log(遠心力)", y = "不定胚の割合") +
scale_color_hue(name = "貯蔵条件", labels = c(cont = "対照群", treat = "処理群"))