Related
I want to generate the 1000 tables from an existing contingency table and estimate the exact p-value by Monte-Carlo. To do so, I know that the contingency tables have to satisfy the condition S = {p(T) ≤ p (array-observe)}. Then, I have to compute the probabilities of unique tables among the 1000 generated.
I have started a part of the code, but I am stuck in generating the tables respecting the condition mentioned above. I looking for a command that could to so without using the command fisher.test.
Here is my code, where infarctus.tables corresponds to the generated tables from a contingency table and infarctus is my data.
infractus <- matrix(c(6, 4, 2, 0, 7, 4, 1, 0, 2,2, 3, 5, 2, 5, 3, 2), nrow = 4, byrow = T)
infarctus.tables ### how do I generate this?
##probabilities of unique tables among the 1000 generated
tables.prob= lapply( unique(infarctus.tables),
FUN=function(x) { exp( a1 - sum(lgamma( x + 1) ) ) })
hist( unlist(tables.prob))
obs.prob= which(unlist(tables.prob) <= 0.0164141415 )
Any idea on how to do so?
Base R includes function r2dtable. From the documentation.
Description
Generate random 2-way tables with given marginals using Patefield's algorithm.
infractus <- matrix(c(6, 4, 2, 0, 7, 4, 1, 0, 2,2, 3, 5, 2, 5, 3, 2), nrow = 4, byrow = T)
rsums <- rowSums(infractus)
csums <- colSums(infractus)
n <- 5
r2dtable(n, rsums, csums)
#> [[1]]
#> [,1] [,2] [,3] [,4]
#> [1,] 3 5 4 0
#> [2,] 6 3 1 2
#> [3,] 3 4 3 2
#> [4,] 5 3 1 3
#>
#> [[2]]
#> [,1] [,2] [,3] [,4]
#> [1,] 8 3 1 0
#> [2,] 1 6 2 3
#> [3,] 4 3 4 1
#> [4,] 4 3 2 3
#>
#> [[3]]
#> [,1] [,2] [,3] [,4]
#> [1,] 4 3 3 2
#> [2,] 5 4 2 1
#> [3,] 6 1 3 2
#> [4,] 2 7 1 2
#>
#> [[4]]
#> [,1] [,2] [,3] [,4]
#> [1,] 4 5 2 1
#> [2,] 4 6 1 1
#> [3,] 7 1 3 1
#> [4,] 2 3 3 4
#>
#> [[5]]
#> [,1] [,2] [,3] [,4]
#> [1,] 3 4 4 1
#> [2,] 5 4 2 1
#> [3,] 7 2 1 2
#> [4,] 2 5 2 3
Created on 2022-10-02 with reprex v2.0.2
To generate 1000 matrices with the given marginals, run
n <- 1000L
infarctus.tables <- r2dtable(n, rsums, csums)
mydata
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3 3 2 3 3 2 3 3 2 3
[2,] 3 3 2 3 3 2 3 3 2 3
[3,] 1 3 2 3 3 2 3 3 2 3
[4,] 1 3 2 3 3 2 3 3 2 3
[5,] 1 3 2 3 3 2 3 3 2 3
I would like to create new data frame with three column by counting the total number of distinct numbers in each row .
The expected outcome should be
>newdata
[,1] [,2] [,3]
[1,] 0 3 7
[2,] 0 3 7
[3,] 1 3 6
[4,] 1 3 6
[5,] 1 3 6
Any help is appreciated.
We can use rep with table
table(rep(seq_len(nrow(mydata)), ncol(mydata)), c(mydata))
data
mydata <- structure(c(3, 3, 1, 1, 1, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3), .Dim = c(5L, 10L))
#Ben provides a great answer, and then the trick is to wrap his table with as.data.frame.matrix() to get the desired result:
# example data
mat <- matrix(c(3,3,1,1,1,rep(3,5),rep(2,5),rep(3,10),
rep(2,5),rep(3,10),rep(2,5),rep(3,5)),
nrow=5, ncol=10)
# count values by row
df <- as.data.frame.matrix(table(row(mat),mat))
# confirm result
str(df)
'data.frame': 5 obs. of 3 variables:
$ 1: int 0 0 1 1 1
$ 2: int 3 3 3 3 3
$ 3: int 7 7 6 6 6
In R, considering a generic matrix A, how can I move the triangle NA to the position in matrix B? What about if the matrix is m x n?
> A <- matrix(c(3, NA, NA, 4, 2, NA, 1, 5, 3), nrow = 3)
> A
[,1] [,2] [,3]
[1,] 3 4 1
[2,] NA 2 5
[3,] NA NA 3
> B <- matrix(c(3, 2, 3, 4, 5, NA, 1, NA, NA), nrow = 3)
> B
[,1] [,2] [,3]
[1,] 3 4 1
[2,] 2 5 NA
[3,] 3 NA NA
Thanks!
I don't know if there's a more idiomatic way, but this seems to do what you want:
A <- matrix(c(3, NA, NA, 4, 2, NA, 1, 5, 3), nrow = 3)
A
[,1] [,2] [,3]
[1,] 3 4 1
[2,] NA 2 5
[3,] NA NA 3
t(apply(A, 1, function(x) x[order(is.na(x))]))
[,1] [,2] [,3]
[1,] 3 4 1
[2,] 2 5 NA
[3,] 3 NA NA
A <- matrix(c(3, NA, NA, NA, 4, 2, NA, NA, 1, 5, 3, NA), nrow=4)
A
[,1] [,2] [,3]
[1,] 3 4 1
[2,] NA 2 5
[3,] NA NA 3
[4,] NA NA NA
t(apply(A, 1, function(x) x[order(is.na(x))]))
[,1] [,2] [,3]
[1,] 3 4 1
[2,] 2 5 NA
[3,] 3 NA NA
[4,] NA NA NA
A <- matrix(c(3, NA, NA, 4, 2, NA, 1, 5, 3, 6, 7, 8), nrow=3)
A
[,1] [,2] [,3] [,4]
[1,] 3 4 1 6
[2,] NA 2 5 7
[3,] NA NA 3 8
t(apply(A, 1, function(x) x[order(is.na(x))]))
[,1] [,2] [,3] [,4]
[1,] 3 4 1 6
[2,] 2 5 7 NA
[3,] 3 8 NA NA
A possible way is to rotate 180 over columns. I assumed that the NA triangle from A is used to set NA in matrix B after rotation. One can use the same technique to rotate in A matrix itself.
A <- matrix(c(3, NA, NA, 4, 2, NA, 1, 5, 3), nrow = 3)
A
[,1] [,2] [,3]
[1,] 3 4 1
[2,] NA 2 5
[3,] NA NA 3
B <- matrix(c(3, 2, 3, 4, 5, 4, 1, 6, 8), nrow = 3)
B
[,1] [,2] [,3]
[1,] 3 4 1
[2,] 2 5 6
[3,] 3 4 8
#Find NA in A
NA_Val <- is.na(A)
#Rotate NA matrix on column
NA_VAL_180 <- NA_Val[1:nrow(NA_Val),ncol(NA_Val):1]
#Set corresponding values NA in B.
B[NA_VAL_180] <- NA
B
[,1] [,2] [,3]
[1,] 3 4 1
[2,] 2 5 NA
[3,] 3 NA NA
I would like to make a cumsum of multiple matrix obtaining the steps. If we consider:
A <- structure(c(1, 2, 3, 2, 3, 1, 4, 1, 2), .Dim = c(3, 3))
# [,1] [,2] [,3]
# [1,] 1 2 4
# [2,] 2 3 1
# [3,] 3 1 2
B <- structure(c(6, 1, 9, 6, 3, 7, 3, 2, 8), .Dim = c(3, 3))
# [,1] [,2] [,3]
# [1,] 6 6 3
# [2,] 1 3 2
# [3,] 9 7 8
C <- structure(c(1, 1, 2, 5, 3, 3, 3, 9, 1), .Dim = c(3, 3))
# [,1] [,2] [,3]
# [1,] 1 5 3
# [2,] 1 3 9
# [3,] 2 3 1
I would like the following results:
[,1] [,2] [,3]
[1,] 1 2 4
[2,] 2 3 1
[3,] 3 1 2
[,1] [,2] [,3]
[1,] 7 8 7
[2,] 3 6 3
[3,] 12 8 10
[,1] [,2] [,3]
[1,] 8 13 10
[2,] 4 9 12
[3,] 14 11 11
with all steps! I could do this with a for loop, but it's slow with big matrix, how can I do this with apply ?
This is a perfect job for Reduce:
Reduce("+", list(A,B,C), accumulate=TRUE)
[[1]]
[,1] [,2] [,3]
[1,] 1 2 4
[2,] 2 3 1
[3,] 3 1 2
[[2]]
[,1] [,2] [,3]
[1,] 7 8 7
[2,] 3 6 3
[3,] 12 8 10
[[3]]
[,1] [,2] [,3]
[1,] 8 13 10
[2,] 4 9 12
[3,] 14 11 11
I have a matrix m and a vector v. I would like to multiply the matrix m into vetcor vand get a matrix whith same dimension as m means that multiply first element of m to v and .... How can I do this in R?
m = matrix(c(1, 2, 3, 4, 5), ncol=1)
v = c(1, 2, 3, 4, 5)
> z
[,1]
[1,] 1
[2,] 4
[3,] 9
[4,] 16
[5,] 25
Cross products can be obtained using the %*% operator:
> m = matrix(c(1, 2, 3, 4, 5), ncol=1)
> v = c(1, 2, 3, 4, 5)
> m %*% v
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 2 4 6 8 10
[3,] 3 6 9 12 15
[4,] 4 8 12 16 20
[5,] 5 10 15 20 25
> m * v
[,1]
[1,] 1
[2,] 4
[3,] 9
[4,] 16
[5,] 25