2017-07-05 70 views
2

これは以前の質問geom_smooth with facet_grid and different fitting functionsに関連する質問です。その質問では、私はgeom_smoothの異なるフィッティング関数をggplot2のファセットグリッドの各ファセットに使用しようとしていました。 Marco Sandriは、既存の数式ではなくユーザー定義の数式を使用するように調整しようとしているという答えを親切に答えました(たとえば、lmloess)。ここに私のコードです。R:nlsがgeom_smoothのカスタム関数で使用されたときに追加の引数を取得しない

# Load library 
library(ggplot2) 

# Load data 
data(mtcars) 

# Smoothing function with different behaviour depending on the panel 
custom.smooth <- function(formula, data,...){ 
    smooth.call <- match.call() 

    if(as.numeric(unique(data$PANEL)) == 6) { 
    # Nonlinear regression 
    method.name <- eval(parse(text="nls")) 
    # Specify formula 
    formula <- as.formula("y ~ a * x^b") 
    # Add initial parameters 
    smooth.call[["start"]] <- c(a = 10, b = -0.5) 
    }else{ 
    # Linear regression 
    method.name <- eval(parse(text="lm")) 
    } 

    # Add function name 
    smooth.call[[1]] <- method.name 
    # Perform fit 
    eval.parent(smooth.call) 
} 

# Plot data with custom fitting function 
p <- ggplot(mtcars,aes(x = disp, y = mpg)) + geom_point() + facet_grid(gear ~ am) 
p <- p + geom_smooth(method = "custom.smooth") 
print(p) 

このコードでは、モデルを選択する関数custom.smoothを定義します。この例では、すべてのモデルがパネル6(ユーザ定義関数y ~ a*x^b)を除いて線形回帰です。私はこれらの初期パラメータを持つパネル6のデータにnlsの実行時に私はこのようなエラー(すなわち、nls(mpg ~ a * disp^b, mtcars %>% filter(gear == 5, am == 1), start = c(a = 10, b = -0.5)))を入手しない、それにもかかわらず

Warning message: Computation failed in stat_smooth() : singular gradient matrix at initial parameter estimates

:このコードを実行すると、エラーが発生します。これにより、nlsに指定した開始値が表示されないと思われます。

p <- p + geom_smooth(method = "custom.smooth", method.args = list(start = c(a = 10, b = -0.5))) 

が、私は同じ問題に遭遇:私はまた、このようなgeom_smooth機能でこれらのパラメータを指定して試してみました。どのように私は私の開始値を得ることができる任意のアイデアnls?あるいは、コードが機能しない別の理由がありますか?

+0

私はそれがあなたの出発価値ではないと思います。モデルはnlsからのデフォルトの開始値(警告を返す)を使用していても収束するだけでなく、開始値を与えずに 'nls'行を' geom_smooth'経由で5 am 1のデータにプロットしても問題ありません。 – aosmith

+0

あなたはそうです。私はそれをチェックしていなかった。うーん。線形フィットが機能するので、式を指定する方法は? – Lyngbakr

答えて

1

ここには、this postという大きなメリットがあります。私は知らないなぜ以前のバージョンは動作しませんでしたが、これはうまくいくようです。

# Load library 
library(ggplot2) 

# Load data 
data(mtcars) 

# Smoothing function with different behaviour depending on the panel 
custom.smooth <- function(formula, data,...){ 
    smooth.call <- match.call() 

    if(as.numeric(unique(data$PANEL)) == 6) { 
    # Nonlinear regression 
    smooth.call[[1]] <- quote(nls) 
    # Specify formula 
    smooth.call$formula <- as.formula("y ~ a * x^b") 
    # Add initial parameters 
    smooth.call$start <- c(a = 300, b = -0.5) 
    }else{ 
    # Linear regression 
    smooth.call[[1]] <- quote(lm) 
    } 

    # Perform fit 
    eval.parent(smooth.call) 
} 

# Plot data with custom fitting function 
p <- ggplot(mtcars,aes(x = disp, y = mpg)) + geom_point() + facet_grid(gear ~ am) 
p <- p + geom_smooth(method = "custom.smooth", se = FALSE) 
print(p) 
+0

新しいフォーミュラでコールを更新しなかったため、以前のバージョンは機能しませんでした。単に「フォーミュラ」という名前の変数を作成しましたが、何もしませんでした。 – MrFlick

+0

Ah。私は関数が 'formula'という変数を渡されていると思ったので、私はそれを変更するだけでした。 – Lyngbakr

+0

あなたの機能は私を大いに助けましたが、今私は別の質問があります。おそらく解決策がありました。この記事をご覧ください:https://stackoverflow.com/questions/48381672/specifying-formula-for-each-facet-using -stat-poly-eq-in-ggplot2 –

関連する問題