Intermediate Regression in R
Richie Cotton
Data Evangelist at DataCamp
A line plot of a quadratic equation
xy_data <- tibble(
x = seq(-4, 5, 0.1),
y = x ^ 2 - x + 10
)
ggplot(xy_data, aes(x, y)) +
geom_line()
$y = x ^ 2 - x + 10$
$\frac{\partial y}{\partial x} = 2 x - 1$
$0 = 2 x - 1$
$x = 0.5$
$y = 0.5 ^ 2 - 0.5 + 10 = 9.75$
calc_quadratic <- function(x) {
x ^ 2 - x + 10
}
optim(par = 3, fn = calc_quadratic)
$par
[1] 0.4998047
$value
[1] 9.75
$counts
function gradient
30 NA
$convergence
[1] 0
$message
NULL
calc_quadratic <- function(coeffs) {
x <- coeffs[1]
x ^ 2 - x + 10
}
optim(par = c(x = 3), fn = calc_quadratic)
$par
x
0.4998047
$value
[1] 9.75
$counts
function gradient
30 NA
$convergence
[1] 0
$message
NULL
optim()
to find coefficients that minimize this function.calc_sum_of_squares <- function(coeffs) {
intercept <- coeffs[1] slope <- coeffs[2]
# More calculation!
}
optim(
par = ???,
fn = ???
)
Intermediate Regression in R