dPrime を計算する関数を作成しました。この関数は、引数としてデータ フレームを受け取ります。これは正常に機能しますが、関数はこれらの特定の名前を使用して dPrime を計算するため、列を「stimDiff」および「stimSame」と呼ぶ必要があります。この関数を複数のサブジェクトを持つデータ フレームに適用し、各サブジェクトの dPrime を計算できるようにしたいと考えています。その結果、各サブジェクトの dPrime スコアを持つ新しいデータ フレームが生成されます。テスト データ フレームは次のようになります。
stimDiff0 <- c(rep("diff", 20), rep("same", 5))
stimSame0 <- c(rep("diff", 10), rep("same", 15))
stimDiff1 <- c(rep("diff", 10), rep("same", 15))
stimSame1 <- c(rep("diff", 10), rep("same", 15))
stimDiff2 <- c(rep("diff", 19), rep("same", 6))
stimSame2 <- c(rep("diff", 11), rep("same", 14))
stimDiff3 <- c(rep("diff", 21), rep("same", 4))
stimSame3 <- c(rep("diff", 9), rep("same", 16))
stimDiff4 <- c(rep("diff", 18), rep("same", 7))
stimSame4 <- c(rep("diff", 12), rep("same", 13))
stimDiff5 <- c(rep("diff", 22), rep("same", 3))
stimSame5 <- c(rep("diff", 14), rep("same", 11))
stimDiff <- c(stimDiff0, stimDiff1, stimDiff2,
stimDiff3, stimDiff4, stimDiff5)
stimSame <- c(stimSame0, stimSame1, stimSame2,
stimSame3, stimSame4, stimSame5)
subject <- rep(0:5, each = 25)
x <- data.frame(subject = subject, stimDiff = stimDiff, stimSame = stimSame)
次のコードを使用して、サブジェクト データ フレームごとに dPrim を取得しようとしています。
tapply(c(x$stimDiff, x$stimSame), x$subject, data = x, FUN = dPrime)
次のエラーが表示されます。
Error en tapply(list(x$stimDiff, x$stimSame), x$subject, data = x, FUN = dPrime) :
arguments must have same length
dPrimeを計算できるパッケージがあることは承知しています。関数の書き方を学ぶためにこれをやっています。ベースRを使用して解決策を見つけたいと思います.
関数 dPrime のコードは次のとおりです。
dPrime <- function(x) {
# Calculate number of same, diff and total responses
# for the stimuli that were actually different
stimDiffRdiff <- nrow(x[x$stimDiff == 'diff', ])
stimDiffRsame <- nrow(x[x$stimDiff == 'same', ])
stimDiffTotal <- length(x$stimDiff)
# Calculate number of same, diff and total responses
# for the stimuli that were actually the same
stimSameRdiff <- nrow(x[x$stimSame == 'diff', ])
stimSameRsame <- nrow(x[x$stimSame == 'same', ])
stimSameTotal <- length(x$stimSame)
# Hit rate = the number of correct responses 'diff'
# when the stimuli were actually diff, divided by
# the total number of responses
hitRate <- stimDiffRdiff / stimDiffTotal
# Miss rate = the number of incorrect responses
# 'same' when the stimuli were actually diff
# divided by the total number of responses
missRate <- stimDiffRsame / stimDiffTotal
# False alarm = the number responses 'diff'
# when the stimuli were actually the same
# divided by the total number of responses
falseAlarm <- stimSameRdiff / stimSameTotal
# Correct rejection = the number of responses
# same when the stimuli were actually the same
# divided by the number of total responses
corrReject <- stimSameRsame / stimSameTotal
# Calculate z-critical values for hit rate
# and false alarm rate
zHitRate <- qnorm(hitRate)
zFalseAlarm <- qnorm(falseAlarm)
# Calculate d prime
dPrime <- zHitRate - zFalseAlarm
print(dPrime)
}