jeudi 5 juillet 2018

why is the information from a subset for-if-loop not accessible?

I'm trying to calculate the hit and false alarm rate, as well as a sensitivity index (d prime). I have three response conditions with 4 parts each, but I'm only intrested in part 3&4.

My data and code look like the following:

response_condition <- c(rep(c(1:3), each=100))
part <- c(rep(rep(c(1:4), times=c(10,10,40,40)), times=3))
response <- rep(c("yes", "no"), 150)
light <- rep(c("yes", "yes", "no", "no"), 75)    
df<-data.frame(condition,part,response,light)

# select the relevant parts of the data
keep<-c(3,4)
df2<-subset(df, part%in%keep)

with(df2,
  # the length of one block of trials within a response condition=80
  n_block=80

  # create vector for hit and false alarms(fa)
  hit <-numeric(lenght=80)
  fa<-numeric(lenght=80)

  # initialize sensitivity index variable
  dprime <- "initialize"

  # select the data e.g. of the second condition.
  # Allocate value 1 to hit if light and response=="yes", 
  # and 1 to fa if light=="no" and response=="yes".
  # Else, both hit and fa are to remain 0
  keep_cond2<-c(2)
  with(subset(df2, response_condition%in%keep_cond2), 
       for (n in 1:80) {
         if ((df2[n, grep("light", df2)] == "yes") && (df2[n, grep("response", df2)] == "yes")) {
         hit[n] <-1}
        else {hit[n]<-0}

        if ((df2[n, grep("light", df2)] == "no") && (df2[n, grep("response", df2)] == "yes")) {
         fa[n] <-1}
        else {fa[n]<-0}}

  # create hit and fa-rate
       hit_rate_2 <- sum(hit)/(n_block)
       fa_rate_2 <- sum(fa)/(n_block)

  # calculate d prime
       dprime2 <-qnorm(hit_rate_2)-qnorm(fa_rate_2) 
  )
)

# collect data in new data frame, which I then use for analyses
final_data <- data.frame(dprime2, hit_rate_2, fa_rate_2)

I receive Error: unexpected symbol in: "hit_rate_2". Furthermore, the information on hit and fa appear to not be available for the hit- and fa-rate calculations. So I subsequently receive a lot of Error...not found as well.

I think the hit and fa value allocation in the for-if-loop is either not happening, or it is not accessible outside of the loop...can someone explain this to me?

Aucun commentaire:

Enregistrer un commentaire