请修改下面代码使它看起来不同,注意修改变量名和函数名:library(gsl) n<-c(20,30,25,30,40,20,50,30,20,20) y<-c(6,1,1,0,5,4,1,8,4,7) p_alpha_beta<-function(alpha,beta,n,y){ a<-exp(alpha)/(1+exp(alpha))^2 b<-1 for(i in 1:length(n)){ x1<-exp(alpha)/(1+exp(-beta))+y[i] y1<-exp(alpha)/(1+exp(beta))+n[i]-y[i] x2<-exp(alpha)/(1+exp(-beta)) y2<-exp(alpha)/(1+exp(beta)) b<-bbeta(x1,y1)/beta(x2,y2) } result<-ab return(result) } estimate_lnk<-function(n,y){ alpha<-rt(length(n),df=4) beta<-rt(length(n),df=4) Num<-sum(alpha*p_alpha_beta(alpha,beta,n,y)/(dt(alpha,df=4)*dt(beta,df=4))) Den<-sum(p_alpha_beta(alpha,beta,n,y)/(dt(alpha,df=4)*dt(beta,df=4))) result<-Num/Den } result<-estimate_lnk(n,y)
时间: 2024-04-28 13:26:14 浏览: 62
下面是修改后的代码,将变量名和函数名进行了修改:
library(gsl)
num_of_success <- c(20, 30, 25, 30, 40, 20, 50, 30, 20, 20)
num_of_trials <- c(6, 1, 1, 0, 5, 4, 1, 8, 4, 7)
calc_prob_alpha_beta <- function(alpha, beta, num_of_success, num_of_trials){
a <- exp(alpha) / (1 + exp(alpha))^2
b <- 1
for(i in 1:length(num_of_trials)){
x1 <- exp(alpha) / (1 + exp(-beta)) + num_of_success[i]
y1 <- exp(alpha) / (1 + exp(beta)) + num_of_trials[i] - num_of_success[i]
x2 <- exp(alpha) / (1 + exp(-beta))
y2 <- exp(alpha) / (1 + exp(beta))
b <- pbeta(x1, y1) / beta(x2, y2)
}
result <- ab
return(result)
}
estimate_log_odds <- function(num_of_success, num_of_trials){
alpha <- rt(length(num_of_trials), df = 4)
beta <- rt(length(num_of_trials), df = 4)
num <- sum(alpha * calc_prob_alpha_beta(alpha, beta, num_of_success, num_of_trials) / (dt(alpha, df = 4) * dt(beta, df = 4)))
den <- sum(calc_prob_alpha_beta(alpha, beta, num_of_success, num_of_trials) / (dt(alpha, df = 4) * dt(beta, df = 4)))
result <- num / den
return(result)
}
log_odds_estimate <- estimate_log_odds(num_of_success, num_of_trials)
阅读全文