2017-08-13 41 views
0

我有兩個數據集。讓我們假設它們看起來像這樣簡單:將建模數據集的分佈與觀測數據集的分佈匹配?

observed <- data.frame(name = c("Jenny", "Mark", "James", "Amber", "Jamie"), 
        height = c(68, 69, 72, 63, 77), 
        mood = c("content", "content", "melancholy", "happy", "melancholy")) 
modeled <- data.frame(name = c("Alex", "Jimmy", "Sal", "Evelyn", "Maria", "George", "Hilary", "Donny", "Jose", "Luke", "Leia"), 
        height = c(74, 71, 68, 66, 80, 59, 67, 67, 69, 65, 72), 
        mood = c("content", "content", "melancholy", "happy", "melancholy","content", "content", "melancholy", "happy", "melancholy", "happy")) 

我想從選擇行建模,使得建模$高度的分佈儘可能接近觀察到$高度的分佈。我需要保持行不變,而不是簡單地匹配高度整數的分佈。任何有識之士將不勝感激。

+0

你的意思是*儘可能接近*?如果你基於'%observed $ height'中的'建模$ height%'來過濾'模型',那麼你將得到完全匹配。這是你想要的嗎? – coffeinjunky

+0

這些數據集很差,無法解決這個問題,因爲它們太小了。我希望高度欄的密度分佈匹配。 –

回答

1

這是非常特別和有一定更好的方法,但這裏有一個:

my_sample <- dplyr::sample_n(modeled, nrow(observed)) 

這裏是他們的樣子,我們做任何事情之前:

plot(density(observed$height)) 
lines(density(my_sample$height), col = "red") 

enter image description here

然後我們挑選更類似的樣本:

while(cor(observed$height, my_sample$height) < .99){ 
    my_sample <- dplyr::sample_n(modeled, nrow(observed)) 
} 

那麼這裏是什麼樣子後:

plot(density(observed$height)) 
lines(density(my_sample$height), col = "red") 

enter image description here

有了更大的數據集,他們更應該相似,其他條件不變。

你甚至可以把它遠一點(或者至少嘗試,並看看是否有在建模數據足夠的變化來退出這個功能):現在

while(cor(observed$height, my_sample$height) < .99 | 
     abs(mean(observed$height) - mean(my_sample$height)) > .5){ 
    my_sample <- dplyr::sample_n(modeled, nrow(observed)) 
} 

,一個問題,您可能會遇到如果您想要的建模樣本必須具有比原始數據集更多的行。其中一種方法是使用不需要樣本長度的平均值和/或其他彙總統計量。另一種方法是使用樣本的樣本,或者從建模數據集的大塊中獲取多個相關性。

相關問題