Я пишу функцию R ниже, в которой missing()
дает мне проблемы. Как прокомментировано в коде, первый ifelse
отлично работает, но не второй. Зачем?Отсутствует() поведение в R
powert <- function(d,a,n1,n2,tl){
H1 <- seq(-3,3,by=.001)
df <- ifelse(missing(n2), n1-1, (n1+n2)-2) # Behaving fine
Hcp <- ifelse(missing(n2),H1*sqrt(n1),H1*sqrt((n1*n2)/(n1+n2))) # !!! MISBEHAVING !!! #
tcrit2 <- ifelse(tl==1 & H1>0,qt(1-a,df),ifelse(tl==2 & H1>0,-qt(a/2,df),ifelse(tl==1 & H1<0,qt(a,df),
ifelse(tl==2 & H1<0,qt(a/2,df),ifelse(tl==1 & H1==0,qt(1-a,df),-qt(a/2,df))))))
TII <- ifelse(tl==1 & H1>0,pt(tcrit2,df,Hcp), ifelse(tl==2 & H1>0,pt(tcrit2,df,Hcp)-pt(-tcrit2,df,Hcp),
ifelse(tl==1 & H1<0,1-pt(tcrit2,df,Hcp),ifelse(tl==2 & H1<0,1-(pt(tcrit2,df,Hcp)+(1-pt(-tcrit2,df,Hcp))),
ifelse(tl==1 & H1==0,1-a,1-a)))))
Pwr <- 1-TII
ESto80 <- if(d>0){H1[min(which(Pwr>=.8 & H1>0))]}
else if(d<0){H1[max(which(Pwr>=.8 & H1<0))]}
else {0}
plot(H1,Pwr,type = "l",las=1,yaxs="i",ylim=c(0,1),lwd=2)
cat("DF behaving fine = ",df)}
## Verify here:
powert(d=.5,a=.05,n1=20,n2=20,tl=2)
Zheyuan, где есть, мужчина? – rnorouzian