Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am struggling with a machine learning course, coding in R required - - which is where my struggle is, the R code. Linear Regression

I am struggling with a machine learning course, coding in R required - - which is where my struggle is, the R code. Linear Regression questions, I need the code for Q2: "Now we will repeat the exercise above but using larger datasets. I need a function that takes a sizen, then (1) builds a dataset using the code provided in Q1 but withnobservations instead of 100 and without theset.seed(1), (2) runs thereplicateloop that you wrote to answer Q1, which builds 100 linear models and returns a vector of RMSEs, and (3) calculates the mean and standard deviation.

Set the seed to 1 (if using R 3.6 or later, use the argumentsample.kind="Rounding") and then usesapplyormapto apply your new function ton <- c(100, 500, 1000, 5000, 10000).

Hint: You only need to set the seed once before running your function; do not set a seed within your function. Also be sure to usesapplyormapas you will get different answers running the simulations individually due to setting the seed.

Mean, 100: 2.507 (correct); SD,100: incorrect;

Mean, 500: incorrect; SD,500: incorrect;

Mean, 1000: incorrect; SD,1000: incorrect;

Mean, 5000: incorrect; SD,5000: incorrect;

Mean, 10000: incorrect; SD,10000: incorrect."

Background, Q1 instructions were: "Make data set using the following code:

set.seed(1) # set.seed(1, sample.kind="Rounding") if using R 3.6 or later n <- 100 Sigma <- 9*matrix(c(1.0, 0.5, 0.5, 1.0), 2, 2) dat <- MASS::mvrnorm(n = 100, c(69, 69), Sigma) %>% data.frame() %>% setNames(c("x", "y")) 

We will build 100 linear models using the data above and calculate the mean and standard deviation of the combined models. First, set the seed to 1 again (make sure to usesample.kind="Rounding"if your R is version 3.6 or later). Then, within areplicateloop, (1) partition the dataset into test and training sets withp=0.5and usingdat$yto generate your indices, (2) train a linear model predictingyfromx, (3) generate predictions on the test set, and (4) calculate the RMSE of that model. Then, report the mean and standard deviation (SD) of the RMSEs from all 100 models.

Report all answers to at least 3 significant digits.

Mean: 2.489 correct; SD: 0.124 correct

My code that is giving me incorrect answers=

f<- function(data) { rmse<- replicate(100, { test_index<- createDataPartition(dat$y, list=FALSE) train_set<- data[-test_index,] test_set<- data[test_index,] fit<- lm(y ~ x, data = train_set) y_hat<- predict(fit, test_set) sqrt(mean((y_hat - test_set$y)^2)) }) structure(c(mean(rmse),sd(rmse)), names=c("mean","sd")) } df<- c(n, mean(n), sd(n)) set.seed(1) n<- c(100, 500, 1000, 5000, 10000) results<- sapply(n, function(n) { Sigma<- 9*matrix(c(1.0, 0.5, 0.5, 1.0), 2, 2) dat<- MASS::mvrnorm(n, c(69,69), Sigma) %>% data.frame() %>% setNames(c("x","y")) }) set.seed(1) results<- map(results,f) results 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Concepts In Set Theory

Authors: Mofidul Islam

1st Edition

9353146232, 9789353146238

More Books

Students also viewed these Mathematics questions

Question

How does depth first traversal work?

Answered: 1 week ago

Question

Is there something else less expensive that would be just as good?

Answered: 1 week ago

Question

How might a variance be traced back to its source? LO6

Answered: 1 week ago