@@ -24,7 +24,7 @@ errorCal <- function(beta0, beta1, x, y){
24
24
errorValues <- c()
25
25
beta1Values <- c()
26
26
count <- 1
27
- for (beta1 in seq(- 100 , 100 , length.out = 1000 ) ){
27
+ for (beta1 in seq(- 100 , 100 , length.out = 300 ) ){
28
28
beta1Values [count ] <- beta1
29
29
errorValues [count ] <- errorCal(0 , beta1 , x , y )
30
30
count <- count + 1
@@ -44,4 +44,45 @@ plot(errorValues, type="l")
44
44
45
45
# We see that the minimum is on zero point.
46
46
min(errorValues )
47
- which(a == min(a ))
47
+ index <- which(errorValues == min(errorValues ))
48
+ beta1Values [index ]
49
+ # #############################################
50
+
51
+
52
+
53
+
54
+ # In the following example we are looking for both beta1 and beta0
55
+ # ############################################
56
+
57
+
58
+ errorValues <- c()
59
+ beta1Values <- c()
60
+ beta0Values <- c()
61
+ count <- 1
62
+
63
+ for (beta0 in seq(- 2 , 2 , length.out = 300 ) ){
64
+
65
+ for (beta1 in seq(- 2 , 2 , length.out = 300 ) ){
66
+ beta0Values [count ] <- beta0
67
+ beta1Values [count ] <- beta1
68
+ errorValues [count ] <- errorCal(beta0 , beta1 , x , y )
69
+ count <- count + 1
70
+ }
71
+ }
72
+
73
+ length(beta1Values )
74
+
75
+ # We see that the minimum is on zero point.
76
+ min(errorValues )
77
+ index <- which(errorValues == min(errorValues ))
78
+ beta1Values [index ]
79
+ beta0Values [index ]
80
+
81
+
82
+ # Now let us visualize this.
83
+ # install.packages("plotly")
84
+ library(plotly )
85
+
86
+
87
+ p <- plot_ly(x = ~ beta0Values , y = ~ beta1Values , z = ~ errorValues , type = ' mesh3d' )
88
+ p
0 commit comments