0

I'm writing a chi-sq stat test in R and my scalar chisq is being converted to a matrix and I can't figure out why.

The variables used here include

k is a scalar

f is a kxk matrix

n is the number of data points being evaluated

chisq is meant to be a scalar but becomes a matrix each time I run this code.

Any help would be appreciated

for(i in 1:k){
    for(j in 1:k){
        chisq<-chisq+(f[i,j]-(n/(k^2)))^2
    }
}
vcelloho
  • 81
  • 1
  • 5

1 Answers1

2

Here is a reproducible chunk of code:

chisq=0
k=3
f=matrix(runif(k*k),k,k)
n=3
for(i in 1:k){
    for(j in 1:k){
        chisq<-chisq+(f[i,j]-(n/(k^2)))^2
    }
}

And chisq comes out as a scalar. If I start with chisq as a matrix, I get a matrix out:

 chisq = matrix(0,1,1)

So, I suspect you are doing the latter, despite what you say. Reproducible examples are there for you as well as us.

Spacedman
  • 92,590
  • 12
  • 140
  • 224