データセットの 91 個の変数すべてを含む依存関係マトリックスを作成する必要があります。
いくつかのコードを使用しようとしましたが、成功しませんでした。
ここにあなたは重要なコードの一部です:
p<- length(dati)
chisquare <- matrix(dati, nrow=(p-1), ncol=p)
すべての変数を含む平方行列を作成する必要があります
system.time({for(i in 1:p){
for(j in 1:p){
a <- dati[, rn[i+1]]
b <- dati[, cn[j]]
chisquare[i, (1:(p-1))] <- chisq.test(dati[,i], dati[, i+1])$statistic
chisquare[i, p] <- chisq.test(dati[,i], dati, i+1])$p.value
}}
})
「p」変数を関連付けて、相互に依存しているかどうかを分析する必要があります
Error in `[.data.frame`(dati, , rn[i + 1]) :
not defined columns selected
Moreover: There are 50 and more alerts (use warnings() to read the first 50)
Timing stopped at: 32.23 0.11 32.69
warnings() #let's check
>: In chisq.test(dati[, i], dati[, i + 1]) :
Chi-squared approximation may be incorrect
chisquare
#すべてのセル (p 値があると思われる最後の列を除く) は、行ごとに同じ値を持ちます
私は別の方法も試しました.Rを私よりもはるかによく管理する方法を知っている人から提供されました:
#strange values I have in some columns
sum(dati == 'x')
#replacing "x" by x
x <- dati[dati=='x']
#distribution of answers for each question
answers <- t(sapply(1:ncol(dati), function(i) table(factor(dati[, i], levels = -2:9), useNA = 'always')))
rownames(answers) <- colnames(dati)
answers
#correlation for the pairs
I<- diag(ncol(dati))
#empty diagonal matrix
colnames(I) <- rownames(I) <- colnames(dati)
rn <- rownames(I)
cn <- colnames(I)
#loop
system.time({
for(i in 1:ncol(dati)){
for(j in 1:ncol(spain)){
a <- dati[, rn[i]]
b <- dati[, cn[j]]
r <- chisq.test(a,b)$statistic
r <- chisq.test(a,b)$p.value
I[i, j] <- r
}
}
})
user system elapsed
29.61 0.09 30.70
There are 50 and more alerts (use warnings() to read the first 50)
warnings() #let's check
-> : In chisq.test(a, b) : Chi-squared approximation may be incorrect
diag(I)<- 1
#result
head(I)
列は 5 番目の変数で停止しますが、すべての変数間の依存関係を確認する必要があります。各自。
どこが間違っているのかわかりませんが、それほど遠くないことを願っています...
よろしければ、どうぞよろしくお願いいたします。