In [1]:
alpha <- 0.05

In [2]:
data <- data.frame(B1=c(10, 11, 6), B2=c(21, 27, 19), B3=c(15, 21, 27), B4=c(6, 13, 24),
                   row.names=c("A1", "A2", "A3"))
data


Out[2]:
B1B2B3B4
A11021156
A211272113
A36192724

In [3]:
a <- nrow(data)
b <- ncol(data)
n <- sum(data)
c(a, b, n)


Out[3]:
  1. 3
  2. 4
  3. 200

In [4]:
p_i <- 1/n * rowSums(data)
p_i


Out[4]:
A1
0.26
A2
0.36
A3
0.38

In [5]:
p_j <- 1/n * colSums(data)
p_j


Out[5]:
B1
0.135
B2
0.335
B3
0.315
B4
0.215

In [6]:
p_i <- 1/n * rowSums(data)
p_i


Out[6]:
A1
0.26
A2
0.36
A3
0.38

In [7]:
p_ij <- p_i %*% t(p_j)
p_ij


Out[7]:
B1B2B3B4
0.03510.08710.08190.0559
0.04860.12060.11340.0774
0.05130.12730.11970.0817

In [8]:
Q_ij <- (data - n*p_ij)^2/(n * p_ij)
Q_ij


Out[8]:
B1B2B3B4
A11.26501420.73572900.11626372.4000358
A20.16855970.34388060.12444440.3973127
A31.76877191.63910450.39112783.5909180

In [9]:
sum(Q_ij)


Out[9]:
12.941162399907

In [10]:
qchisq(1-alpha, (a-1)*(b-1))


Out[10]:
12.591587243744

$Q > \chi^2 \implies$ H0 wird verworfen