Exercise - LR and Out of Sample Prediction?Generate 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 noise?Construct 3 models: one linear model with no variables, one with all the variables and one with only the variable V99?Compute the MSE of each model?Hint: 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 noise?y ?Pick from your data only 1/5th random observations?Use the remaining 4/5th observations to rebuild the three models?Make prediction on the 1/5th observations?What 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 data?Find three models you might think are meaningful for the prediction with different number of variables?Compute the in-sample mean squared error and the R squared?Compute 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 validation?Which wine would you buy now?Hint: the skeleton for cross validation is provided.In?[4]:wine.white y ## Skeleton ####################################################SEED # data # y # K # # # set.seed(SEED)##代寫LR留學(xué)生作業(yè)、代做R程序設(shè)計作業(yè)锰茉、代寫Sample Prediction作業(yè)呢蔫、代做R語言作業(yè) 代寫留學(xué)生 Statn #predictions ###ii #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 轉(zhuǎn)自:http://www.3zuoye.com/contents/18/4805.html
講解:LR捆蜀、R疮丛、Sample Prediction、R Statistics辆它、誊薄、|C/C
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門认然,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人漫萄,你說我怎么就攤上這事卷员。” “怎么了腾务?”我有些...
- 文/不壞的土叔 我叫張陵毕骡,是天一觀的道長。 經(jīng)常有香客問我窑睁,道長挺峡,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任担钮,我火速辦了婚禮橱赠,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘箫津。我一直安慰自己狭姨,他們只是感情好,可當我...
- 文/花漫 我一把揭開白布苏遥。 她就那樣靜靜地躺著饼拍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪田炭。 梳的紋絲不亂的頭發(fā)上师抄,一...
- 文/蒼蘭香墨 我猛地睜開眼灭返,長吁一口氣:“原來是場噩夢啊……” “哼景用!你這毒婦竟也來了涵叮?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布诚些,位于F島的核電站,受9級特大地震影響皇型,放射性物質(zhì)發(fā)生泄漏诬烹。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一弃鸦、第九天 我趴在偏房一處隱蔽的房頂上張望绞吁。 院中可真熱鬧,春花似錦唬格、人聲如沸家破。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽汰聋。三九已至,卻和暖如春喊积,著一層夾襖步出監(jiān)牢的瞬間烹困,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- Exercise - LR and Out of Sample Prediction?Generate 99 in...
- Exercise - LR and Out of Sample Prediction?Generate 99 in...
- Exercise - LR and Out of Sample Prediction?Generate 99 in...
- 文/壹見 很高興遇見你! 某個瞬間抒巢,你是否感到贫贝,生活的節(jié)奏不再跟著你轉(zhuǎn),你原以為自己是中心蛉谜,卻生生活成一個刺猬稚晚。 ...