Importando libreria y datos
library(MASS)
str(Boston)
## 'data.frame': 506 obs. of 14 variables:
## $ crim : num 0.00632 0.02731 0.02729 0.03237 0.06905 ...
## $ zn : num 18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
## $ indus : num 2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
## $ chas : int 0 0 0 0 0 0 0 0 0 0 ...
## $ nox : num 0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
## $ rm : num 6.58 6.42 7.18 7 7.15 ...
## $ age : num 65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
## $ dis : num 4.09 4.97 4.97 6.06 6.06 ...
## $ rad : int 1 2 2 3 3 3 5 5 5 5 ...
## $ tax : num 296 242 242 222 222 222 311 311 311 311 ...
## $ ptratio: num 15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
## $ black : num 397 397 393 395 397 ...
## $ lstat : num 4.98 9.14 4.03 2.94 5.33 ...
## $ medv : num 24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
Creando función para encontrar los B, predicciones y_hat y R2.
Mlm <- function(dframe) {
#Variables Predictoras. Mantiene todas las columnas excepto la primera que es la var a predecir en una matriz.
X = as.matrix(dframe[-1])
#Almacena la variable Y en una matriz(Crim en este caso del dataset Boston)
y = as.matrix(dframe[,1])
#Agrega columna de 1s al principio de las features
X = cbind(rep(1, length(X[,1])),X)
#Calculando coeficientes > B's
Bs <- solve(t(X) %*% X) %*% t(X) %*% y
Bs <- round(Bs, 2)
yhat = X%*%Bs
R2 = sum((yhat-mean(y))^2)/sum((y-mean(y))^2)
result = list(Bs, R2)
return(result)
}
Activando función. Nos muestra los coeficientes para cada variable y el R2
Mlm(Boston)
## [[1]]
## [,1]
## 17.03
## zn 0.04
## indus -0.06
## chas -0.75
## nox -10.31
## rm 0.43
## age 0.00
## dis -0.99
## rad 0.59
## tax 0.00
## ptratio -0.27
## black -0.01
## lstat 0.13
## medv -0.20
##
## [[2]]
## [1] 0.5861857