講解:LR捆蜀、R疮丛、Sample Prediction、R Statistics辆它、誊薄、|C/C

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市飒筑,隨后出現(xiàn)的幾起案子片吊,更是在濱河造成了極大的恐慌,老刑警劉巖协屡,帶你破解...
    沈念sama閱讀 212,222評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件俏脊,死亡現(xiàn)場離奇詭異,居然都是意外死亡肤晓,警方通過查閱死者的電腦和手機爷贫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,455評論 3 385
  • 文/潘曉璐 我一進店門认然,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人漫萄,你說我怎么就攤上這事卷员。” “怎么了腾务?”我有些...
    開封第一講書人閱讀 157,720評論 0 348
  • 文/不壞的土叔 我叫張陵毕骡,是天一觀的道長。 經(jīng)常有香客問我窑睁,道長挺峡,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,568評論 1 284
  • 正文 為了忘掉前任担钮,我火速辦了婚禮橱赠,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘箫津。我一直安慰自己狭姨,他們只是感情好,可當我...
    茶點故事閱讀 65,696評論 6 386
  • 文/花漫 我一把揭開白布苏遥。 她就那樣靜靜地躺著饼拍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪田炭。 梳的紋絲不亂的頭發(fā)上师抄,一...
    開封第一講書人閱讀 49,879評論 1 290
  • 那天扰才,我揣著相機與錄音衙傀,去河邊找鬼檀训。 笑死茬腿,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的姑廉。 我是一名探鬼主播逐样,決...
    沈念sama閱讀 39,028評論 3 409
  • 文/蒼蘭香墨 我猛地睜開眼灭返,長吁一口氣:“原來是場噩夢啊……” “哼景用!你這毒婦竟也來了涵叮?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,773評論 0 268
  • 序言:老撾萬榮一對情侶失蹤伞插,失蹤者是張志新(化名)和其女友劉穎割粮,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體媚污,經(jīng)...
    沈念sama閱讀 44,220評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡穆刻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,550評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了杠步。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片氢伟。...
    茶點故事閱讀 38,697評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖幽歼,靈堂內(nèi)的尸體忽然破棺而出朵锣,到底是詐尸還是另有隱情,我是刑警寧澤甸私,帶...
    沈念sama閱讀 34,360評論 4 332
  • 正文 年R本政府宣布诚些,位于F島的核電站,受9級特大地震影響皇型,放射性物質(zhì)發(fā)生泄漏诬烹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,002評論 3 315
  • 文/蒙蒙 一弃鸦、第九天 我趴在偏房一處隱蔽的房頂上張望绞吁。 院中可真熱鬧,春花似錦唬格、人聲如沸家破。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,782評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽汰聋。三九已至,卻和暖如春喊积,著一層夾襖步出監(jiān)牢的瞬間烹困,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,010評論 1 266
  • 我被黑心中介騙來泰國打工乾吻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留髓梅,地道東北人。 一個月前我還...
    沈念sama閱讀 46,433評論 2 360
  • 正文 我出身青樓溶弟,卻偏偏與公主長得像女淑,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子辜御,可洞房花燭夜當晚...
    茶點故事閱讀 43,587評論 2 350

推薦閱讀更多精彩內(nèi)容