联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2024-03-31 06:26

Machine Learning, 2023 - 2024

Assignment 1

DUE DATE: April 10th, 2024

Instructions: Endeavor to approach your analysis with precision and depth.   While  you are encouraged to discuss the problem set with peers and utilize existing literature or notes to deepen your understanding, the code you write and the analysis you perform must be your own.  Compose your responses using original wording, and include well-commented code that reflects your personal thought process. Alongside your code, provide a clear interpretation of the results, explaining what they signify in the context of the problem at hand. This approach will demonstrate your proficiency in both the computational and theoretical aspects of the assignment.

1 Lasso and Ridge

Please refer to James et al. (2013) Section 6.6.

In this section, you will learn how to perform Least Absolute Shrinkage and Selection Operator (Lasso) and ridge regression in R. We will use the Hitters data to predict a baseball player’s Salary on the basis of various statistics associated with performance in the previous year.

1. Necessary  Packages: Begin by installing and loading the necessary package.   You will need the ISLR package to approach the Hitters data. Use the install.packages function to install the package, and then load it with the library function.

2. Remove Observations with Missing Values: Use the na.omit() function to remove all of the rows that have missing values in any variable.

3. Set X and y: Set X as a matrix which contains the variables except for Salary using the model.matrix() function, and set y a vector, which stands for Salary.

4. Grid of λ: You will need to run cross-validation to find the best lambda value for ridge and Lasso regression. Set the grid of λ to be a geometric progression {1010 , 109.87878788 ··· , 10 ?2 }.Number of λ=100

5. Data Splitting: Set a seed for reproducibility using the set.seed() function with a chosen number (e.g., 39). Split the samples in half: 50 % of the samples are in training set and the other 50 % of which are in test set.

6. Ridge Regression:

(a)  Run the 10-folds cross-validation to find the best λ value for ridge regression.  Utilize the cv.glmnet function from the glmnet package, setting alpha to 0.

(b) After determining the best λ, fit the ridge model using this value.

(c)  Then, make predictions on your test dataset and calculate the Mean Squared Error (MSE) to evaluate the performance of the model.

(d)  Refit the ridge regression model on the full data set, using the value of λ chosen by cross-validation, and examine the coefficient estimates.

7. Lasso Regression: Similar to Question 6, follow its instructions to run a Lasso regression. The only difference is to set alpha to 1.

8. Compare Ridge and Lasso: Compare the coefficient estimates from  Question 6(d) and Question 7(d). What do you find?

2    Simulation Study

In this section, you will learn how to perform Least Absolute Shrinkage and Selection Operator (Lasso) regression and Ordinary Least Squares  (OLS)  regression in R. We will start with the mathematical model behind these regression methods and then proceed to the step-by-step R implementation.

The OLS regression model is given by the equation:

y = Xβ + ?

where y is the dependent variable, X is the matrix of independent variables, β is the vector of coefficients, and ? is the error term.

Lasso regression adds a penalty equal to the absolute value of the magnitude of coefficients to the loss function:

where λ is the regularization parameter that controls the strength of the penalty.   The  Lasso technique encourages simple, sparse models (i.e., models with fewer parameters).

Parameter Setup

Before starting the coding process, set up the parameters and simulation for your model.  In the parameter setup, we will specify the model configuration and simulate the dataset.  We choose a number of observations n = 100 and a number of predictors p = 10.  The predictor matrix X  is simulated by drawing from a normal distribution with mean 0 and standard deviation 1, resulting in a 100×10 matrix of random values.  A true coefficient vector β is constructed with chosen values such as (3, 1.5, 0, 0, 2, 0, 0, 0, 0, 0), where non-zero values correspond to the influential predictors in the model.  Finally, the response variable y  is generated according to the equation y = Xβ + ?, where ? represents normally distributed noise with a mean of 0 and a standard deviation of 1.  This noise term adds a stochastic element to our linear model to more accurately reflect the variability encountered in real-world data.

1. Necessary  Packages: Begin by installing and loading the necessary packages.  You will need the glmnet package to perform Lasso regression and the caret package to facilitate data splitting.  Use the install.packages function to install each package, and then load them with the library function.

2. Data  Simulation  and  Splitting: Set  a seed for reproducibility using the  set.seed() function with a chosen number  (e.g.,  42).   Simulate  your dataset  as per the instructions provided in the Parameter Setup section of this exercise. Once your dataset is ready, split it into training and test sets. Use the createDataPartition function from the caret package to create indices for splitting, ensuring that 80% of the data is used for training and the remaining 20% for testing.  Extract the training and test sets for both the predictor matrix X and the response variable y.

3. Lasso Regression: You will need to run cross-validation to find the best lambda value for Lasso regression.  Utilize the cv.glmnet function from the glmnet package, setting alpha to 1.  After determining the best lambda, fit the Lasso model using this value.  Then, make predictions on your test dataset and calculate the Mean Squared Error (MSE) to evaluate the performance of the model.

4. Ordinary Least Squares Regression: For  comparison, fit an Ordinary Least Squares (OLS) regression model without an intercept using the lm function. Predict the response on the test dataset using the fitted OLS model and calculate the MSE for these predictions.

5. Comparison and Conclusion: After fitting both models, it’s important to compare their performance. Calculate the MSE on the test set for both the Lasso and OLS models.  Discuss which model provides better predictions and consider the effects of regularization in the Lasso model during your comparison.  Create a table to summarize the MSE results for each model for a clear comparison.

Hints and Tips:

. Always set a random seed before simulating data to ensure that your results can be repro- duced.

.  Scale your features before applying Lasso, especially if they are on different scales, to ensure that the regularization is applied uniformly.

. Use cross-validation to find the optimal regularization parameter, lambda, for Lasso to avoid underfitting or overfitting.

.  The predict function in R is very handy for making predictions with your fitted models on new data. It’s a good practice to become familiar with its use.



版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp