2017-10-04 2 views
-3

Question to be answered計算係数

誰もが2行のコードで接続の問題を解決する方法を知っていますか?私は、as.matrixが行列Xを作成し、次に答えを得るのにX %*% Xt(X)、およびsolve(X)を使うと信じています。しかし、それは動作していないようです。どんな答えでも助けてくれます。

+1

これまでに試したことをお見せください。 [再現可能な例](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)を作成すると便利です –

答えて

1

私はあなたがこのスレッドでは2つの関数の違いを乗り越えすることは有用であろうread.csv代わりのread.table

を使用することをお勧めします:@kon_uの回答に基づいてread.csv vs. read.table

df <- read.csv("http://pengstats.macssa.com/download/rcc/lmdata.csv") 
model1 <- lm(y ~ x1 + x2, data = df) 
coefficients(model1) # get the coefficients of your regression model1 
summary(model1) # get the summary of model1 
0

df <- read.csv("http://pengstats.macssa.com/download/rcc/lmdata.csv") 
model1 <- lm(y ~ x1 + x2, data = df) 
coefficients(model1) # get the coefficients of your regression model1 
summary(model1) # get the summary of model1 

### Based on the formula 
X <- cbind(1, df$x1, df$x2) # the column of 1 is to consider the intercept 
Y <- df$y 
bhat <- solve(t(X) %*% X) %*% t(X) %*% Y # coefficients 
bhat # Note that we got the same coefficients with the lm function 
+0

この数式を使用すると、多変量線形回帰の係数も計算します。 – ANG