2010-03-15 79 views
1

這是一個簡單的隨機實驗。R中的隨機實驗

在下面的代碼中,我計算了零假設下的p值,即施用於番茄植株的兩種不同肥料對植物產量沒有影響。 第一個隨機樣本(x)來自使用標準肥料的植物,而第二個樣本(y)來自的植物使用了「改良」樣本。

x <- c(11.4,25.3,29.9,16.5,21.1) 
y <- c(23.7,26.6,28.5,14.2,17.9,24.3) 
total <- c(x,y) 
first <- combn(total,length(x)) 
second <- apply(first,2,function(z) total[is.na(pmatch(total,z))]) 
dif.treat <- apply(second,2,mean) - apply(first,2,mean) 
# the first element of dif.treat is the one that I'm interested in 
(p.value <- length(dif.treat[dif.treat >= dif.treat[1]])/length(dif.treat)) 

你知道任何R函數執行這樣的測試嗎?

編輯

# this is the equivalent independent t.test 
t.test(x,y,alternative = "less",var.equal = T) 

回答

4

boot庫便於引導和置換檢驗,但它不會進行詳細的測試(這是OK的大部分時間)。 coin庫也實現了精確的隨機化測試。

+0

謝謝!我在{BHH2}中發現了一些「permtest」。它似乎以非常相似的方式工作。巧合的是,使用相同的番茄數據集作爲例子! – 2010-03-15 19:48:25

+2

Deducer也做monte carlo置換測試:perm.t.test(x,y,「mean」,「less」) – 2010-03-15 20:28:13