Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the code to be modify 1. In the lecture, we wrote an R function to apply the batch gradient descent algorithm to fit

image text in transcribed

image text in transcribed

Here is the code to be modify

image text in transcribed

1. In the lecture, we wrote an R function to apply the batch gradient descent algorithm to fit a linear regression model describing the relationship between the variables dist and speed in the cars data. Modify that function to make it implement the stochastic gradient descent algorithm to solve the same probem. Compare the results and computing time for the two algorithms (batch versus stochastic gradient descent). To ensure a fair comparison, make sure to use the same inputs for both algorithms (initial values, learning rate, convergence threshhold, and maximum number of iterations). For these paramters, use the samee inputs we used in the lecture. In your comparison, include the following: a. How close are the estimated coefficients from each method to the coefficients obtained from the built-in im function. system.time({gd. lreg (speed, dist, 0.001, le-10, 1000000) }) gd. lreg max.iter) { converged = TRUE return (cat("Intercept at last iter:", bo, ' ', "Slope at last iter:", bi,' ', "MSE at last iter values:", MSE_new)) #Run the function on the cars data gd.lreg (speed, dist, 0.001, le-10, 1000000) 1. In the lecture, we wrote an R function to apply the batch gradient descent algorithm to fit a linear regression model describing the relationship between the variables dist and speed in the cars data. Modify that function to make it implement the stochastic gradient descent algorithm to solve the same probem. Compare the results and computing time for the two algorithms (batch versus stochastic gradient descent). To ensure a fair comparison, make sure to use the same inputs for both algorithms (initial values, learning rate, convergence threshhold, and maximum number of iterations). For these paramters, use the samee inputs we used in the lecture. In your comparison, include the following: a. How close are the estimated coefficients from each method to the coefficients obtained from the built-in im function. system.time({gd. lreg (speed, dist, 0.001, le-10, 1000000) }) gd. lreg max.iter) { converged = TRUE return (cat("Intercept at last iter:", bo, ' ', "Slope at last iter:", bi,' ', "MSE at last iter values:", MSE_new)) #Run the function on the cars data gd.lreg (speed, dist, 0.001, le-10, 1000000)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions