Exercise - LR and Out of Sample PredictionGenerate 99 independent variables uniformly distributed between -100 and 100 of size 100 observations each.Generate the dependent variable y = 3 + 10*V99, where V99 is the last covariate and add some noiseConstruct 3 models: one linear model with no variables, one with all the variables and one with only the variable V99Compute the MSE of each modelHint: for the first two points code is provided below.In [1]:set.seed(123)n p x ## Generate the output variable as a linear combination of x## With jitter() you add random noisey Pick from your data only 1/5th random observationsUse the remaining 4/5th observations to rebuild the three modelsMake prediction on the 1/5th observationsWhat do you observe now?Hint: for the first point code is provided.In [2]:## Pick randomly 1/5th of observastionsii ## Built a test and training setdata.te data.tr y.te y.tr Exercise - Part 2Now:Build 99 different models including from 1 to 99 input variables on training data (4/5th observations)For each model compute the out-of-sample MSE on the remaining 1/5th (test data)Plot the out-of-sample MSE as a function of the number of variablesHint: you may prefer to use a for-loop.Exercise Cross ValidationWe are interested in predicting the quality of wines using chemical indicators. To do so, we have a disposal two data sets for white and red wine, reporting the variable quality on a scale from 0 to 10.white wine dataFind three models you might think are meaningful for the prediction with different number of variablesCompute the in-sample mean squared error and the R squaredCompute the out-of-sample mean squared error using a test-training set approach (remember to set the seeds)Compute the out-of-sample mean squared error using 10-folds cross validationWhich wine would you buy now?Hint: the skeleton for cross validation is provided.In [4]:wine.white y #for (i in 1:K) {# hold # train ## ## Build model ## ## Store the predictions for the left-out segment# predictions[hold] #}## Calculate estimated MSPE#mean((y - predictions)^2)Ridge RegressionWe are interested in predicting the level of alchol consumption during the weekend for students, controlling for many social and academic indicators. Some of them are the average grades for three years, the income of the family, the age, etc. In total we have 32 variables, but we want to find just the ones most correlated with alchol consumption.We will explore the linear mode, the ridge regression and lasso.Do the following:Download the student txt fileNote: the dependent variable is Walc (Week-end alchol consumption)In [ ]:student Explore the variables and construct two different linear models. You can use any specification you think is most appropriate. Provide justifications.Report the interpretation of the coefficientsRidge Regression:Construct a sequence of lambda from to Use cross validation to find the best lambda to be used for estimating ridge regression (use the skeleton provided in the hints of the previous exercises)Construct a ridge regression with the lambda with minimum errorHint: Code for the first two points is provided.Model comparison:Use cross validation to compare the linear models that you choose and the ridge regression.Do you think it is the correct way to compare the models?In [ ]:## Hint code for the first part of the exercise## Expand matrixxm y ## Use this functions to standardizestandard_for_dummy { return(1)} return(sd(k)) }sd.tr mu_for_dummy { return(0.5)} mean(k) }mu.tr ## New covariate matrixxmn ## Set your lambda lambdas.rr 转自:http://www.6daixie.com/contents/18/4922.html
讲解:Sample Prediction、data、R、RSQL|SQL
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Exercise - LR and Out of Sample PredictionGenerate 99 in...
- By clicking to agree to this Schedule 2, which is hereby ...
- Exercise - LR and Out of Sample PredictionGenerate 99 in...
- Android的SharedPreferences用来存储一些键值对, 但是却不支持跨进程使用.跨进程来用的话, ...