解释合并散点图(R)并计算均值方差

时间:2020-07-11 22:00:17

标签: r ggplot2 scatter-plot bin variance

我试图将模拟结果与样本作图。因此,我有许多数据点-因此我选择了合并散点图。在其中一篇论文中,建议对合并的图进行绘制,以便通过计算着色点的方差来计算一阶效应。 我的情节看起来像这样:

我从这里使用此代码的代码: making binned scatter plots for two variables in ggplot2 in R

但是,我不太了解如何解释剧情。我知道色点是垃圾箱的平均值,但是它实际上告诉我们有关数据的信息,以及如何进一步计算这些黄点的方差?

我们可以从该图中暗示即使某些黄色点并未真正遵循趋势,变量仍显示(弱)线性关系吗?

提前谢谢!

Binned scatter plot

1 个答案:

答案 0 :(得分:1)

我们可以通过AppDelegate函数对数据进行分档,

library://

然后计算合并数据的平均值,

class MediaPlayerUtilities {
    public static let LocalLibraryIdentifierPrefix = "library://"

    private class func searchForPodcastInLocalLibrary(withPredicate predicate: MPMediaPropertyPredicate) -> MPMediaItem? {
        let mediaQuery = MPMediaQuery.podcasts()
        mediaQuery.addFilterPredicate(predicate)

      return mediaQuery.items?.first
    }

    class func searchForPodcastInLocalLibrary(byName podcastName: String) -> MPMediaItem? {
        let predicate = MPMediaPropertyPredicate(value: podcastName, forProperty: MPMediaItemPropertyPodcastTitle)
        return searchForPodcastInLocalLibrary(withPredicate: predicate)
    }

    class func searchForPodcastInLocalLibrary(byPersistentID persistentID: UInt64) -> MPMediaItem? {
        let predicate = MPMediaPropertyPredicate(value: persistentID, forProperty: MPMediaItemPropertyPersistentID)
        return searchForPodcastInLocalLibrary(withPredicate: predicate)
    }
}

要将我们的结果与@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var player: AVPlayer? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Set our podcast title in AppIntentVocabulary.plist so we get the proper Siri intent. // In your app, you'll want to make this dynamically tuned to a user's podcast titles. let vocabulary = INVocabulary.shared() let podcastNames = NSOrderedSet(objects: "The Talk Show with John Gruber") vocabulary.setVocabularyStrings(podcastNames, of: .mediaShowTitle) INPreferences.requestSiriAuthorization { (status) in print(status) } return true } func handlePlayMediaIntent(_ intent: INPlayMediaIntent, completion: @escaping (INPlayMediaIntentResponse) -> Void) { // Extract the first media item from the intent's media items (these will have been resolved in the extension). guard let mediaItem = intent.mediaItems?.first, let identifier = mediaItem.identifier else { return } // Check if this media item is a podcast and if it's identifier has the local library prefix. if mediaItem.type == .podcastShow, let range = identifier.range(of: MediaPlayerUtilities.LocalLibraryIdentifierPrefix) { // Extract the persistentID for the local podcast and look it up in the library. guard let persistentID = UInt64(identifier[range.upperBound...]), let podcast = MediaPlayerUtilities.searchForPodcastInLocalLibrary(byPersistentID: persistentID) else { return } guard let podcastURL = podcast.assetURL else { return } // Set the player queue to the local show. player = AVPlayer(url: podcastURL) DispatchQueue.main.async { self.player?.play() } } else { print("ERROR with finding in library.") } completion(INPlayMediaIntentResponse(code: .success, userActivity: nil)) } // This method is called when the application is background launched in response to the extension returning .handleInApp. func application(_ application: UIApplication, handle intent: INIntent, completionHandler: @escaping (INIntentResponse) -> Void) { guard let playMediaIntent = intent as? INPlayMediaIntent else { completionHandler(INPlayMediaIntentResponse(code: .failure, userActivity: nil)) return } handlePlayMediaIntent(playMediaIntent, completion: completionHandler) } } 的{​​{1}}函数进行比较,我们可以将它们绘制在一起,

cut()

enter image description here

现在,要计算方差,我们可以使用mybin <- cut(df$x,20,include.lowest=TRUE,right = FALSE) df$Bins <- mybin 函数简单地遵循相同的过程。所以,

library(tidyverse)

out<- df %>% group_by(Bins) %>% summarise(x=mean(x),y=mean(y)) %>% as.data.frame()

给出合并数据的方差。注意,由于x轴是装仓的,因此x的方差几乎为零。因此,这里重要的是实际上y轴的方差。

  • 合并数据的方差使我们对数据的heteroscedasticity进行了模拟。

  • 合并平均值的路径也显示了数据的模式。因此,您的数据呈积极趋势。 (无需查看完美的平滑线)。但是由于您所建议的方法不同,它变得更弱。

数据:

stat_summary_bin()

注意:数据和某些ggplot2代码是从OP的question中提取的。