This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

原本求出的曲線code

############## mod1__ROC   ##################

nll.fvsd=function(par,y)
{
  d=par[1]
  sigma=par[2]
  c=par[3]
  p=1:4
  p[1]=1-pnorm(c,d,sigma)  #probability of hit
  p[2]=1-p[1]
  p[3]=1-pnorm(c,0,1)      #probability of fa
  p[4]=1-p[3]
  return(-sum(y*log(p)))
}

#restricted model(dA,sigma,cA,cB,cC,cD)

nll=function(par6,dat)
{
  nll.fvsd(par6[c(1,2,3)],dat[1:4])+
    nll.fvsd(par6[c(1,2,4)],dat[5:8])+
    nll.fvsd(par6[c(1,2,5)],dat[9:12])+
    nll.fvsd(par6[c(1,2,6)],dat[13:16])
}

dat=c(1,7,7,8,15,47,2,52,60,65,3,169,179,26,4,155)

par6=c(1.2,0.2,0,0,0,0)


mod1=optim(par6,nll,dat=dat)
mod1
## $par
## [1] 1.7758507 1.0588589 0.3617923 2.9921489 1.9696476 1.0476496
## 
## $value
## [1] 300.213
## 
## $counts
## function gradient 
##      501       NA 
## 
## $convergence
## [1] 1
## 
## $message
## NULL
##ROC圖
hit.rate=c(0.0025,0.04,0.19,0.6375,1)    #累進
fa.rate=c(0.0175,0.0225,0.03,0.04,1)        #累進

原本求出的曲線圖

用for迴圈找曲線上點

###建立一個空矩陣
P<-data.frame(matrix(NA,length(seq(-10,100)),2))
colnames(P)=c("fa","hit")
####### 迴圈 ######
for(i in seq(-10,100,0.1))
{
  nll.fvsd=function(par,y)
  {
    d=par[1]
    sigma=par[2]
    c=par[3]
    p=1:4
    p[1]=1-pnorm(c,d,sigma)  #probability of hit
    p[2]=1-p[1]
    p[3]=1-pnorm(c,0,1)      #probability of fa
    p[4]=1-p[3]
    return(-sum(y*log(p)))
  }
  
  #restricted model(dA,sigma,cA,cB,cC,cD)
  
  nll=function(par6,dat)
  {
    nll.fvsd(par6[c(1,2,3)],dat[1:4])+
      nll.fvsd(par6[c(1,2,4)],dat[5:8])+
      nll.fvsd(par6[c(1,2,5)],dat[9:12])+
      nll.fvsd(par6[c(1,2,6)],dat[13:16])
  }
  
  dat=c(1,7,7,8,15,47,2,52,60,65,3,169,179,26,4,155)
  
  par6=c(1.2,0.2,0,0,0,0)
  
  
  mod1=optim(par6,nll,dat=dat)
  mod1

    hit=1-pnorm(i,mod1$par[1],mod1$par[2])
    fa=1-pnorm(i,0,1)
        P$fa[i]=fa
         P$hit[i]=hit

}

點做圖

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.