0
我正在研究5種鳥開始蛻皮的一年中的某一天和這5種物種完成羽毛換羽的天數之間的相關性。通過自舉法計算相關係數
我試着在下面的代碼中模擬我的數據。對於這5種物種中的每一個,我都有10個人的開始日期和10個人的持續時間。對於每個物種,我計算了平均開始日期和平均持續時間,然後計算了這5種物種間的相關性。
我想要做的是引導平均開始日期和引導每個物種的平均持續時間。我想重複這個10,000次,並在每次重複之後計算相關係數。然後我想提取10,000個相關係數的0.025,0.5和0.975分位數。
儘管模擬原始數據,但我的代碼很快就變得凌亂了,一旦我嘗試引導。誰能幫我這個?
# speciesXX_start_day is the day of the year that 10 individuals of birds started moulting their feathers
# speciesXX_duration is the number of days that each individuals bird took to complete the moulting of its feathers
species1_start_day <- as.integer(rnorm(10, 10, 2))
species1_duration <- as.integer(rnorm(10, 100, 2))
species2_start_day <- as.integer(rnorm(10, 20, 2))
species2_duration <- as.integer(rnorm(10, 101, 2))
species3_start_day <- as.integer(rnorm(10, 30, 2))
species3_duration <- as.integer(rnorm(10, 102, 2))
species4_start_day <- as.integer(rnorm(10, 40, 2))
species4_duration <- as.integer(rnorm(10, 103, 2))
species5_start_day <- as.integer(rnorm(10, 50, 2))
species5_duration <- as.integer(rnorm(10, 104, 2))
start_dates <- list(species1_start_day, species2_start_day, species3_start_day, species4_start_day, species5_start_day)
start_duration <- list(species1_duration, species2_duration, species3_duration, species4_duration, species5_duration)
library(plyr)
# mean start date for each of the 5 species
starts_mean <- laply(start_dates, mean)
# mean duration for each of the 5 species
durations_mean <- laply(start_duration, mean)
# correlation between start date and duration
cor(starts_mean, durations_mean)
非常感謝代碼,這真的很有用。我想知道如何用'boot'包做同樣的事情? – luciano
@luciano我從來沒有使用過這個包,但這裏有一個很好的教程/示例:http://www.statmethods.net/advstats/bootstrapping.html。你基本上必須定義你的相關性計算函數,並通過boot()提供你的數據。從概念上講,這基本上是一樣的過程。 –