【问题标题】:Bootstrapping 4x4 Matrix with Fixed Row and Column Sums使用固定的行和列总和引导 4x4 矩阵
【发布时间】:2014-05-11 13:30:18
【问题描述】:

我想知道是否可以在保持恒定行和列总和的同时对 4x4 数据集进行洗牌。诚然,我是编程初学者,所以我在下面包含的代码可能并不容易。

任何帮助将不胜感激,谢谢。

PS:如果你必须知道,数据集是基于种族的汽车偏好调查。

CarPreference <- read.table ( text = "
African 3 0 1 1
Asian 2 1 0 1
Hispanic 0 1 3 1
White 0 1 4 1
" )

row.names(CarPreference) <- CarPreference[,1]
colnames(CarPreference) <-c("Car Type","Car","Truck","SUV","Motorcycle")

CarPreference <- CarPreference[,-1]
as.matrix(CarPreference)

observed <- rbind(c(3,0,1,1),c(2,1,0,1),c(0,1,3,1),c(0,1,4,1))
deals=10000
observed.boot = array(NA,c(4,4,deals))
H0 <- c(rep(1,colSums(observed)[1]),rep(0,colSums(observed)[2]),rep(1,colSums(observed)[3]),rep(0,colSums(observed)[4]))
for (i in 1:deals)
{
data.boot <- sample(H0,sum(observed),replace=FALSE)

row1.boot <- data.boot[1:rowSums(observed)[1]]
row2.boot <- data.boot[(rowSums(observed)[1]+1):(rowSums(observed)[1]+rowSums(observed)[2])]
row3.boot <- data.boot[(rowSums(observed)[1]+rowSums(observed)[2]+1):(rowSums(observed)[1]+rowSums(observed)[2]+rowSums(observed)[3])]
row4.boot <- data.boot[(rowSums(observed)[1]+rowSums(observed)[2]+rowSums(observed)[3]+1):sum(observed)]

col1.boot <- data.boot[1:colSums(observed)[1]]
col2.boot <- data.boot[(colSums(observed)[1]+1):(colSums(observed)[1]+colSums(observed)[2])]
col3.boot <- data.boot[(colSums(observed)[1]+colSums(observed)[2]+1):(colSums(observed)[1]+colSums(observed)[2]+colSums(observed)[3])]
col4.boot <- data.boot[(colSums(observed)[1]+colSums(observed)[2]+colSums(observed)[3]+1):sum(observed)]

observed.boot[,,i] <- rbind(
c(sum(row1.boot),length(row1.boot)-sum(row1.boot), , ),
c(sum(row2.boot),length(row2.boot)-sum(row2.boot), , ),
c(sum(row3.boot),length(row3.boot)-sum(row3.boot), , ),
c(sum(row4.boot),length(row4.boot)-sum(row4.boot), , ))
}

【问题讨论】:

  • 随机播放是什么意思?您的意思是要使用当前矩阵中的确切数字(4 0、8 1、1 2、2 3 和 1 4)或任何整数,只要行和列的总和等于原始矩阵?这是否需要在满足您要求的所有矩阵的空间内是随机的?
  • 相反,我的意思是每行/列中所有值的总和保持不变。在这个矩阵中,第 1 行的总和 = 5,第 2 行的总和 = 4,等等。类似地,col 1 = 5,col 2 = 3,等等。我真正想做的是 Fisher 精确测试的引导版本4x4 矩阵。我可以为 2x2 执行此操作,但不知道如何在 4x4 的“observed.boot”部分中随机生成数字,以便满足这些条件。
  • 我查看了置换测试,这很相似,但不是我希望实现的。尽管排列的数量是有限的,但我希望每个“排列”都保持行和列的总和。

标签: r


【解决方案1】:

归结起来,您希望随机打乱观察的行标签,同时保持它们的列标签相同。您可以通过构建所有列索引的向量 y 并反复对其进行洗牌来做到这一点:

set.seed(144)
observed <- rbind(c(3,0,1,1),c(2,1,0,1),c(0,1,3,1),c(0,1,4,1))
x <- rep(1:nrow(observed), rowSums(observed))
y <- rep(1:ncol(observed), colSums(observed))
samples <- lapply(1:10000, function(a) table(x, sample(y)))

现在,samples 包含一个引导表列表,其中行和列总和与 observed 匹配。

samples[[1]] 
# x   1 2 3 4
#   1 1 1 2 1
#   2 0 0 2 2
#   3 2 0 2 1
#   4 2 2 2 0
samples[[10000]]
# x   1 2 3 4
#   1 1 1 2 1
#   2 2 1 1 0
#   3 1 1 2 1
#   4 1 0 3 2

这与从一组列联表中随机抽样相同,这些列联表的行和列总和与原始表相同。

【讨论】:

  • 哇!这正是我在这么几行中所希望的。我是这个网站的新手,我怎样才能给你留下积极的反馈/投票?另外,只是想知道您是否做了 set.seed(144) 因为 4x4 和 12^2=144 中有 12 个单元...?
  • @user3624658 不,我只是设置了种子,所以它可以重现。我认为在获得 15 名声望之前你不能投票 :)
猜你喜欢
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
相关资源
最近更新 更多