数式は、Rの統計関数とグラフ関数の非常に便利な機能です。みんなのように、私はこれらの関数のユーザーです。ただし、数式オブジェクトを引数として取る関数を作成したことはありません。Rプログラミングのこちら側への読みやすい紹介にリンクするか、自己完結型の例を示すことによって、誰かが私を助けてくれるかどうか疑問に思いました。
3055 次
1 に答える
7
とを使用model.matrix()
しmodel.frame()
て、式を評価できます。
lm1 <- lm(log(Volume) ~ log(Girth) + log(Height), data=trees)
print(lm1)
form <- log(Volume) ~ log(Girth) + log(Height)
# use model.matrix
mm <- model.matrix(form, trees)
lm2 <- lm.fit(as.matrix(mm), log(trees[,"Volume"]))
print(coefficients(lm2))
# use model.frame, need to add intercept by hand
mf <- model.frame(form, trees)
lm3 <- lm.fit(as.matrix(data.frame("Intercept"=1, mf[,-1])), mf[,1])
print(coefficients(lm3))
これは
Call: lm(formula = log(Volume) ~ log(Girth) + log(Height), data = trees)
Coefficients: (Intercept) log(Girth) log(Height)
-6.63 1.98 1.12
(Intercept) log(Girth) log(Height)
-6.632 1.983 1.117
Intercept log.Girth. log.Height.
-6.632 1.983 1.117
于 2009-08-19T16:00:11.503 に答える