2015-10-12 21 views
5

散布図があるので、信頼区間の上と下の遺伝子をどのように見つけることができますか?ggplot2でgeom_stat/geom_smoothを使用したときの信頼区間以下の点を見つけよう

enter image description here


EDIT:再現例:

library(ggplot2) 
#dummy data 
df <- mtcars[,c("mpg","cyl")] 

#plot 
ggplot(df,aes(mpg,cyl)) + 
    geom_point() + 
    geom_smooth() 

enter image description here

+7

あなたはあなたのコードとデータを含むことによって開始することができます。 – nrussell

+0

'identify(x、y ...)'しかし、データの一部が必要です – Mateusz1981

+0

信頼区間の行は、データそのものではなく、データの平均に対する信頼区間です。そして、あなたは非常に多くのデータを持っているので、私はその値の大半がその区間の外にあることを期待しています。 – bramtayl

答えて

7

私はgithubレポに深いダイビングを取らなければならなかったが、私は最終的にそれを得ました。これを行うには、stat_smoothの仕組みを知る必要があります。この特定のケースではloess機能は、(異なる平滑化機能は、以下のように同じプロセスを使用して構成することができる)の平滑化を行うために呼び出されます。

だから、私たちはどうなるこの機会にloessを使用して:

#data 
df <- mtcars[,c("mpg","cyl"), with=FALSE] 
#run loess model 
cars.lo <- loess(cyl ~ mpg, df) 

stat_smoothで予測がどのように行われているかを確認するには、thisを読まなければなりませんでした。

predictdf.loess <- function(model, xseq, se, level) { 
    pred <- stats::predict(model, newdata = data.frame(x = xseq), se = se) 

    if (se) { 
    y = pred$fit 
    ci <- pred$se.fit * stats::qt(level/2 + .5, pred$df) 
    ymin = y - ci 
    ymax = y + ci 
    data.frame(x = xseq, y, ymin, ymax, se = pred$se.fit) 
    } else { 
    data.frame(x = xseq, y = as.vector(pred)) 
    } 
} 

私が使用して、予測の私自身のdata.frameを作成することができた上記を読んだ後:我々の場合のために、次のように明らかにハドレーは(名前空間にエクスポートされません)predictdf機能を使用しています

#get the predictions i.e. the fit and se.fit vectors 
pred <- predict(cars.lo, se=TRUE) 
#create a data.frame from those 
df2 <- data.frame(mpg=df$mpg, fit=pred$fit, se.fit=pred$se.fit * qt(0.95/2 + .5, pred$df)) 

predictdf.loessを見ると、信頼区間の上限はpred$fit + pred$se.fit * qt(0.95/2 + .5, pred$df)、下限はpred$fit - pred$se.fit * qt(0.95/2 + .5, pred$df)と作成されています。私たちはそれらの境界上または下の点のためのフラグを作成することができ、それらを使用して

#make the flag 
outerpoints <- +(df$cyl > df2$fit + df2$se.fit | df$cyl < df2$fit - df2$se.fit) 
#add flag to original data frame 
df$outer <- outerpoints 

df$outer列はOPが探しているものはおそらくあり、それは外にある場合(これは、1の値をとりますそれ以外の場合は0)が、私はそれを下にプロットしています。

上記の+関数は、ここでは論理フラグを数値に変換するためにのみ使用されています。

今、私たちはこのようプロット場合:

ggplot(df,aes(mpg,cyl)) + 
    geom_point(aes(colour=factor(outer))) + 
    geom_smooth() 

私たちは、実際には信頼区間の内側と外側でのポイントを見ることができます。

出力:

enter image description here

P.S.上下の境界に興味がある人のために、彼らはこのように作成されている(投機:網掛け部分はおそらくgeom_ribbonで作成されているが - または類似した何か - それより丸くきれいにする):

#upper boundary 
ggplot(df,aes(mpg,cyl)) + 
    geom_point(aes(colour=factor(outer))) + 
    geom_smooth() + 
    geom_line(data=df2, aes(mpg , fit + se.fit , group=1), colour='red') 

#lower boundary 
ggplot(df,aes(mpg,cyl)) + 
    geom_point(aes(colour=factor(outer))) + 
    geom_smooth() + 
    geom_line(data=df2, aes(mpg , fit - se.fit , group=1), colour='red') 
+1

いいえ、比較回答を投稿しようとしていました;-) – Jaap

+0

ありがとう@ジャップ:)。申し訳ありませんが、私はそれが経験からどのようなものか知っています。あなたがそれが追加情報を追加すると思うなら投稿してください。 – LyzandeR

+1

必要はありませんが、私はあなたの答えを改善するために何も持っていません:-)(いくつかの小さな編集の外に) – Jaap

8

このソリューションは、ハードワークのggplot2を活用して、あなたのために行います。

library(sp) 

# we have to build the plot first so ggplot can do the calculations 
ggplot(df,aes(mpg,cyl)) + 
    geom_point() + 
    geom_smooth() -> gg 

# do the calculations 
gb <- ggplot_build(gg) 

# get the CI data 
p <- gb$data[[2]] 

# make a polygon out of it 
poly <- data.frame(
    x=c(p$x[1], p$x, p$x[length(p$x)], rev(p$x)), 
    y=c(p$ymax[1], p$ymin, p$ymax[length(p$x)], rev(p$ymax)) 
) 

# test for original values in said polygon and add that to orig data 
# so we can color by it 
df$in_ci <- point.in.polygon(df$mpg, df$cyl, poly$x, poly$y) 

# re-do the plot with the new data 
ggplot(df,aes(mpg,cyl)) + 
    geom_point(aes(color=factor(in_ci))) + 
    geom_smooth() 

enter image description here

それは(その最後のポイントは2値を取得IE)微調整のビットを必要としますが、私は時間に制限されたんです。

  • 0:ポイントは
  • 1をPOLに厳密に外装です:ポイントは、POL
  • 2に厳密インテリアです:ポイントは、POL
  • のエッジの相対的な内部に位置 point.in.polygon戻り値であることに注意してください
  • 3:ポイントはそれだけでにコードを変更する簡単なはずのpol

の頂点であります/FALSE値が0であるかどうか。

6

@ hrbrmstrの素晴らしいソリューションのようにggplot_buildを使用すると、エラーの境界を計算する場所を指定するx値のシーケンスをgeom_smoothに渡し、これをポイントのx値と同じにするだけで実際に行うことができます。次に、y値が範囲内にあるかどうかを確認するだけです。

library(ggplot2) 

## dummy data 
df <- mtcars[,c("mpg","cyl")] 

ggplot(df, aes(mpg, cyl)) + 
    geom_smooth(params=list(xseq=df$mpg)) -> gg 

## Find the points within bounds 
bounds <- ggplot_build(gg)[[1]][[1]] 
df$inside <- with(df, bounds$ymin < cyl & bounds$ymax > cyl) 

## Add the points 
gg + geom_point(data=df, aes(color=inside)) + theme_bw() 

enter image description here

関連する問題