You are on page 1of 6

HW2 - Problem 4.2.

Man-fang Liang

2023-03-31

Fit the signal-detection model to the data in Table 3.4.


Helper function: Individual condition negative log-likelihood function

#negative log likelihood for signal detection


#par=c(d’,c)
#y=c(hit,miss,fa,cr)
nll.sd=function(par,y)
{
p=1:4
p[1]=1-pnorm(par[2],par[1],1)
p[2]=1-p[1]
p[3]=1-pnorm(par[2],0,1)
p[4]=1-p[3]
return(-sum(y*log(p)))
}

General model
Fit a general model with separate parameters for each condition.

𝑌ℎ,𝑖 ∼ 𝐵(1 − Φ(𝑐𝑖 − 𝑑𝑖′ ), 𝑁𝑠,𝑖 ),

𝑌𝑓,𝑖 ∼ 𝐵(1 − Φ(𝑐𝑖 ), 𝑁𝑛,𝑖 )

′ ′ ′ ′ ′
With separate parameters for each condition, the general model has ten parameters: 𝑑𝐴 , 𝑐𝐴 , 𝑑𝐵 , 𝑐𝐵 , 𝑑𝐶 , 𝑐𝐶 , 𝑑𝐷 , 𝑐𝐷 , 𝑑𝐸 , 𝑐𝐸 .

Common-sensitivity model (Restricted)


Fit a common-sensitivity model.
𝑌ℎ,𝑖 ∼ 𝐵(1 − Φ(𝑐𝑖 − 𝑑′ ), 𝑁𝑠,𝑖 ),
𝑌𝑓,𝑖 ∼ 𝐵(1 − Φ(𝑐𝑖 ), 𝑁𝑛,𝑖 )

The common sensitivity model has six parameters: 𝑑′ , 𝑐𝐴 , 𝑐𝐵 , 𝑐𝐶 , 𝑐𝐷 , 𝑐𝐸 .

1
#negative log likelihood for Model 2:
#assign par6=d,cA,cB,cC,cD,cE
#assign y20=(h1,m1,f1,c1,h2,m2,f2,c2...)
nll.sd.2=function(par6,y20)
{
nll.sd(par6[1:2],y20[1:4])+
nll.sd(par6[c(1,3)],y20[5:8])+
nll.sd(par6[c(1,4)],y20[9:12])+
nll.sd(par6[c(1,5)],y20[13:16])+
nll.sd(par6[c(1,6)],y20[17:20])
}

MLE
Read data from table 3.4.

dat <- as.integer(unlist(strsplit("404 96 301 199 348 152 235 265 287 213 183 317 251 249 102 398 148 35

# reshape the data vector into a matrix


dat_mat <- matrix(dat, byrow=T, nrow=5)

Estimate parameters for both models.

# General model
# par=rep(0.5, 10) #starting values
# mod1=optim(par,nll.1,y20=dat,hessian=T)
mod1_loglike <- 0.0
mod1_par <- c()
genSE <- c()

for (i in 1:5) {
mod <- optim(c(1, 0),
nll.sd,
y=dat_mat[i, ],hessian=T
)

mod1_loglike <- mod1_loglike + mod$value

mod1_par <- c(mod1_par,


mod$par
)
genSE <- c(genSE, sqrt(diag(solve(mod$hessian)))[1])

# Common-sensitivity model
par=rep(0.5, 6) #starting values
mod2=optim(par,nll.sd.2,y20=dat,hessian=T)

2
# general model
# mod1_par <- mod1$par
names(mod1_par) <- c('dA\'', 'cA', 'dB\'', 'cB', 'dC\'', 'cC', 'dD\'', 'cD', 'dE\'', 'cE')
mod1_par

## dA' cA dB' cB dC' cC


## 0.61178216 -0.25868042 0.58796685 0.07502907 0.52882625 0.34230652
## dD' cD dE' cE
## 0.83273536 0.82766759 1.21451879 1.75068599

# Common-sensitivity model
mod2_par <- mod2$par
names(mod2_par) <- c('d\'', 'cA', 'cB', 'cC', 'cD', 'cE')
mod2_par

## d' cA cB cC cD cE
## 0.7096255 -0.2165652 0.1327715 0.4343349 0.7586419 1.3946258

Make a graph showing the sensitivity parameters for each condition


(from the general model) with standard errors.

# helper function: error bars


errbar=function(x,y,height,width,lty=1){
arrows(x,y,x,y+height,angle=90,length=width,lty=lty)
arrows(x,y,x,y-height,angle=90,length=width,lty=lty)
}

plotdat <- mod1_par[c(1,3,5,7,9)]

xpos=barplot(plotdat,
names.arg=c('A', 'B', 'C', 'D', 'E'),
col='lightblue',ylim=c(0,1.5),space=.5,
xlab='Conditions',
ylab='Sensitivity parameters')
errbar(xpos,plotdat,genSE,.3)

3
1.2
Sensitivity parameters

0.8
0.4
0.0

A B C D E

Conditions

Plot the data as an ROC and add a line denoting the common-
sensitivity model.

# helper function: calculate ph and pf


ph_hat <- function(row) {
return(row[1] / (row[1] + row[2]))
}

pf_hat <- function(row) {


return(row[3] / (row[3] + row[4]))
}

Data points

# calculate ROC data points


ROC_data_pf <- apply(dat_mat, 1, pf_hat)
ROC_data_ph <- apply(dat_mat, 1, ph_hat)

4
Common-sensitivity model line

# The sensitivity parameter estimated by common-sensitivity model


mod2$par[1]

## [1] 0.7096255

Φ−1 (𝑝ℎ,𝑖̂ ) = Φ−1 (𝑝𝑓,𝑖̂ ) + 𝑑′ ,

where 𝑑′̂ = 0.71

d_hat <- mod2$par[1]


g <- seq(-2, 2, 0.01)
ph <- g + d_hat

plot

par(pty="s")

plot(qnorm(ROC_data_pf),qnorm(ROC_data_ph),ylab="z(Hit Rate)",
xlab="z(False-Alarm Rate)", pch=16 , asp = 1,ylim=c(-2,2),xlim=c(-2,2))

lines(g, ph)
legend('bottomright', legend=c("Data points", "Model prediction (d'=0.71)"), col=c("black", "black"), pc

5
2
1
z(Hit Rate)

0
−1

Data points
Model prediction (d'=0.71)
−2

−2 −1 0 1 2

z(False−Alarm Rate)

Perform a likelihood ratio test to see if sensitivity varies across


conditions.
𝐿𝑟 (𝑝𝑟̂ ; 𝑦)
𝐺2 = −2 log ( )
𝐿𝑔 (𝑝𝑔̂ ; 𝑦)

′ ′ ′ ′ ′
Null-hypothesis: 𝑑𝐴 = 𝑑𝐵 = 𝑑𝐶 = 𝑑𝐷 = 𝑑𝐸

# G-squared
G2 <- 2*(mod2$value-mod1_loglike)
G2

## [1] 30.92364

# critical value of the chi-square statistic


critical.val <- qchisq(0.95, 5-1)
critical.val

## [1] 9.487729

Under the null-hypothesis, 𝐺2 should be distributed as a chi-square. 𝐺2 is 30.92, larger than the critical
value of 9.49 with 4 degrees of freedom. The null hypothesis has been rejected. The sensitivity parameter
statistical significantly varies across conditions.

You might also like