web-dev-qa-db-ja.com

格子グラフィックスに回帰直線を使用してxyplotをプロットする

ラティスパッケージをロードしました。それから私は走ります:

> xyplot(yyy ~ xxx | zzz, panel = function(x,y) { panel.lmline(x,y)}

これにより、xyplotを使用せずに、回帰直線を示すプロットのパネルが生成されます。私はそれがどのように行われるかを完全に理解せずにpanel.lmlineを行っています。データ引数があることを知っています。データは何ですか。3つの変数xxxyyyzzzがあることを知っていますか?

12
Selvam

本当に必要なのは:

_xyplot(yyy ~ xxx | zzz, type = c("p","r"))
_

ここで、type引数は_?panel.xyplot_に記載されています

私はそれをすべて引用しませんが

_type: character vector consisting of one or more of the following:
      ‘"p"’, ‘"l"’, ‘"h"’, ‘"b"’, ‘"o"’, ‘"s"’, ‘"S"’, ‘"r"’,
      ‘"a"’, ‘"g"’, ‘"smooth"’, and ‘"spline"’.  If ‘type’ has more
      than one element, an attempt is made to combine the effect of
      each of the components.

      The behaviour if any of the first six are included in ‘type’
      is similar to the effect of ‘type’ in ‘plot’ (type ‘"b"’ is
      actually the same as ‘"o"’).  ‘"r"’ adds a linear regression
      line (same as ‘panel.lmline’, except for default graphical
      parameters). ‘"smooth"’ adds a loess fit (same as
      ‘panel.loess’).  ‘"spline"’ adds a cubic smoothing spline fit
      (same as ‘panel.spline’).  ‘"g"’ adds a reference grid using
      ‘panel.grid’ in the background (but using the ‘grid’ argument
      is now the preferred way to do so).  ‘"a"’ has the effect of
      calling ‘panel.average’, which can be useful for creating
      interaction plots.  The effect of several of these
      specifications depend on the value of ‘horizontal’.
_

上で示したように、typeに文字ベクトルを渡すことで、これらを直列に追加できます。基本的に、コードは_type = "r"_と同じ結果をもたらしました。つまり、のみ回帰直線が描画されました。

panelxyplot引数、および一般的なラティスプロット関数は非常に強力ですが、非常に複雑なものに必ずしも必要なわけではありません。基本的に、プロットの各パネルに物を描画する関数panelを渡す必要があります。必要なことを実行するようにコードを変更するには、panel.xyplot()への呼び出しも追加する必要があります。例えば。:

_xyplot(yyy ~ xxx | zzz,
       panel = function(x, y, ...) {
                 panel.xyplot(x, y, ...)
                 panel.lmline(x, y, ...)
               })
_

_..._を介して個々のパネル関数で他のすべての引数を渡すことも非常に便利です。この場合、無名関数の引数として_..._が必要です(上記を参照)。実際、おそらくそのパネル関数部分は次のように書くことができます。

_xyplot(yyy ~ xxx | zzz,
       panel = function(...) {
                 panel.xyplot(...)
                 panel.lmline(...)
               })
_

しかし、私は通常、明確にするためにx引数とy引数を追加します。

23
Gavin Simpson