2017-08-13 39 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)) 
} 

,一个问题,您可能会遇到如果您想要的建模样本必须具有比原始数据集更多的行。其中一种方法是使用不需要样本长度的平均值和/或其他汇总统计量。另一种方法是使用样本的样本,或者从建模数据集的大块中获取多个相关性。

相关问题