2017-12-20 13 views
10

をの中でweights引数を使って呼び出すと、奇妙な動作が発生しました。`lapply`の` weights`引数で `lm`を呼び出す際にエラーが発生しました。

私のコードは、lapplyで呼び出す線形モデルを実行する式のリストで構成されています。これまでのところ、それが働いていた:

dd <- data.frame(y = rnorm(100), 
       x1 = rnorm(100), 
       x2 = rnorm(100), 
       x3 = rnorm(100), 
       x4 = rnorm(100), 
       wg = runif(100,1,100)) 

ls.form <- list(
    formula(y~x1+x2), 
    formula(y~x3+x4), 
    formula(y~x1|x2|x3), 
    formula(y~x1+x2+x3+x4) 
) 

res.no.wg <- lapply(ls.form, lm, data = dd) 

をしかし、私はweights引数を追加するとき、私は奇妙なエラーが出ます:

res.with.wg <- lapply(ls.form, lm, data = dd, weights = dd[,"wg"]) 
Error in eval(extras, data, env) : 
    ..2 used in an incorrect context, no ... to look in 

それはlapplyから...lm...と競合している場合のようなものですただし、引数のためだけです。

この問題の原因は何ですか?どのように修正するのですか?

注:予想通りlapply作品なしで呼び出しを使用して:

lm(ls.form[[1]], data = dd, weights = dd[,"wg"]) 

Call: 
lm(formula = ls.form[[1]], data = dd, weights = dd[, "wg"]) 

Coefficients: 
(Intercept)   x1   x2 
    -0.12020  0.06049  -0.01937 

EDIT最終呼び出しはタイプのfunctionlapplyです:

f1 <- function(samp, dat, wgt){ 
res.with.wg2 <- lapply(ls.form, function(x) {lm(formula = x, data=dat[samp,], weights=dat[samp,wgt])}) 
} 

f1(1:66, dat=dd, wgt = "wg") 
+0

これが問題のようです'wem'の関数で' lm'を使うと、以下のように表示されます:https://stackoverflow.com/questions/38683076/ellipsis-trouble-passing-to-lm –

+0

質問があってもこれを再オープンしました。元の質問に対する回答よりもここでの回答が良いので、前に尋ねられます。 https://stackoverflow.com/questions/33479862/use-a-weights-argument-in-a-list-of-lm-lapply-calls –

+0

@ G.Grothendieckは、両方の質問を一緒にリンクさせることが重要ではありませんduping経由で?多分これで他のものを捨てるだろうか? – Sotos

答えて

5

匿名関数ラッパーを使用する。

更新

ためのモデル式を変更するこの具体的な問題は、あなたの代わりにupdateを使用してlapplylmを呼び出す避けるために書き換えることができます。

# create base model (the formula here doesn't really matter, but we can specify the weights safely here) 
baselm <- lm(y+x1,data=dd,weights=dd[,"wg"]) 
# update with lapply 
lapply(ls.form,update,object=baselm) 
[[1]] 

Call: 
lm(formula = y ~ x1 + x2, data = dd, weights = dd[, "wg"]) 

Coefficients: 
(Intercept)   x1   x2 
    0.07561  0.16111  0.15014 

... 
+0

興味深い、私はすべてのコードを混在させる必要があります私はそれを試してみます。それがどのようになったのかを教えてあげます。 – Bastien

+0

最後に、あなたのソリューションはこの質問にはいいですが、私の問題は解決しません(問題を別の場所に移したと思います...)。私はあなたの答えを受け入れましたが、 /stackoverflow.com/questions/47909470/calling-update-within-a-lapply-within-a-function-why-isnt-it-working – Bastien

11

私はなぜそれが機能していないのか分かりませんが、あなたのための修正があると思います:

res.with.wg2 <- lapply(ls.form, 
        function(x) {lm(formula = x, data=dd, weights=dd[,"wg"])}) 

希望します。

For historical reasons, the calls created by lapply are unevaluated, and code has been written (e.g., bquote) that relies on this. This means that the recorded call is always of the form FUN(X[[i]], ...), with i replaced by the current (integer or double) index. This is not normally a problem, but it can be if FUN uses sys.call or match.call or if it is a primitive function that makes use of the call. This means that it is often safer to call primitive functions with a wrapper, so that e.g. lapply(ll, function(x) is.numeric(x)) is required to ensure that method dispatch for is.numeric occurs correctly.

lmはそのオープニングラインで二回match.callを使用しています:ソリューションは、ヘルプファイルに記載

cl <- match.call() 
mf <- match.call(expand.dots = FALSE) 

と@Florianがあることでlapplyのヘルプファイル内のノートがあり

+0

私の質問にあなたのコメントを削除すべきではない、あなたが提供したリンクは有用だった – Bastien

+0

こんにちは@Bastien、コメントを削除して申し訳ありません。私はそれが実際の問題(その答えの下に長いコメントのスレッドがあったかどうか)、特に以下の@Jamesの答えを読むときにはわからなかったので、私はそれを削除しました。私はまだ問題を理解しようとしていますが、それは私の頭の上に少し上がるのではないかと恐れています。本当に私を混乱させるのは、 'res.with.wg Florian

+0

不思議なことに、[この問題は](https://stackoverflow.com/questions/33479862/use-a-weights-argument-in-a-list-of-lm-lapply-calls)です。それは私の問題と同じくらい正確に同じ答えを含んでいます... – Florian

関連する問題