2017-10-04 4 views
-3

Question to be answeredBerechnung Koeffizienten bivariate lineare Regression

Wer weiß, wie das beigefügte Problem in zwei Zeilen Code zu lösen? Ich glaube, dass eine as.matrix arbeiten würde, um eine Matrix zu erstellen, X, und dann X %*% X, t(X) und solve(X) zu verwenden, um die Antwort zu erhalten. Es scheint jedoch nicht zu funktionieren. Jede Antwort wird helfen, danke.

+1

Bitte zeigen Sie uns, was Sie bisher versucht haben. Es wäre hilfreich für Sie, ein [reproduzierbares Beispiel] zu erstellen (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). –

Antwort

1

würde ich mit read.csv statt read.table

empfehlen Es wäre nützlich für Sie über den Unterschied der beiden Funktionen in diesem Thread zu gehen: 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

Basierend auf der Antwort von @kon_u Hier ist ein Beispiel, um es von Hand zu tun:

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

Beachten Sie, dass diese Formel Ihnen erlaubt Berechnen Sie auch Koeffizienten multivariater linearer Regressionen. – ANG