Skip to content

Commit 4e8e284

Browse files
Kia TeymourianKia Teymourian
Kia Teymourian
authored and
Kia Teymourian
committed
finished M3E4
1 parent 98b788a commit 4e8e284

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

Module-3-Example-4.R

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ errorCal <- function(beta0, beta1, x, y){
2424
errorValues <-c()
2525
beta1Values <-c()
2626
count<-1
27-
for (beta1 in seq(-100, 100, length.out = 1000) ){
27+
for (beta1 in seq(-100, 100, length.out = 300) ){
2828
beta1Values[count] <- beta1
2929
errorValues[count] <- errorCal(0, beta1, x , y)
3030
count <- count + 1
@@ -44,4 +44,45 @@ plot(errorValues, type="l")
4444

4545
# We see that the minimum is on zero point.
4646
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

Comments
 (0)