library(scatterplot3d)
x1 <- c(1.9,0.8,1.1,0.1,-0.1,4.4,4.6,1.6,5.5,3.4)
x2 <- c(66, 62, 64, 61, 63, 70, 68, 62, 68, 66)
y <- c(0.7,-1.0,-0.2,-1.2,-0.1,3.4,0.0,0.8,3.7,2.0)
dataset = cbind.data.frame(x1,x2,y)
# Regresión:
my.lm<- lm(y ~ x1 + x2,data=dataset)
scatterplot3d(x1,x2,y)

ls <- function(dataset, par)
{with(dataset, sum((y-par[1]-par[2]*x1-par[3]*x2)^2))}
result <- optim(par=c(0,0,0),ls,data=dataset)
# starting values for the parameters to be optimised (0,0,0)
coef <- result$par
coef
## [1] -11.4591902 0.4500494 0.1726229
y_hat=my.lm$fitted.values
plot3d <- scatterplot3d(x1,x2,y,
angle=55, scale.y=0.7, pch=16, color ="red", main ="Regression Plane")
plot3d$plane3d(my.lm, lty.box = "solid")
plot3d$points3d(x1,x2,y_hat,col="blue", type="h", pch=16)

stargazer(my.lm,type="html")
|
|
|
|
Dependent variable:
|
|
|
|
|
|
y
|
|
|
|
x1
|
0.450
|
|
|
(0.414)
|
|
|
|
|
x2
|
0.173
|
|
|
(0.271)
|
|
|
|
|
Constant
|
-11.453
|
|
|
(16.795)
|
|
|
|
|
|
|
Observations
|
10
|
|
R2
|
0.663
|
|
Adjusted R2
|
0.567
|
|
Residual Std. Error
|
1.125 (df = 7)
|
|
F Statistic
|
6.887** (df = 2; 7)
|
|
|
|
Note:
|
p<0.1; p<0.05; p<0.01
|