講解:Sample Prediction枫甲、data、R扼褪、RSQL|SQL

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 #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.6daixie.com/contents/18/4922.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末想幻,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子话浇,更是在濱河造成了極大的恐慌举畸,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,222評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件凳枝,死亡現(xiàn)場離奇詭異抄沮,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)岖瑰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,455評論 3 385
  • 文/潘曉璐 我一進(jìn)店門叛买,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蹋订,你說我怎么就攤上這事率挣。” “怎么了露戒?”我有些...
    開封第一講書人閱讀 157,720評論 0 348
  • 文/不壞的土叔 我叫張陵椒功,是天一觀的道長捶箱。 經(jīng)常有香客問我,道長动漾,這世上最難降的妖魔是什么丁屎? 我笑而不...
    開封第一講書人閱讀 56,568評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮旱眯,結(jié)果婚禮上晨川,老公的妹妹穿的比我還像新娘。我一直安慰自己删豺,他們只是感情好共虑,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,696評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著呀页,像睡著了一般妈拌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蓬蝶,一...
    開封第一講書人閱讀 49,879評論 1 290
  • 那天供炎,我揣著相機(jī)與錄音,去河邊找鬼疾党。 笑死,一個(gè)胖子當(dāng)著我的面吹牛惨奕,可吹牛的內(nèi)容都是我干的雪位。 我是一名探鬼主播,決...
    沈念sama閱讀 39,028評論 3 409
  • 文/蒼蘭香墨 我猛地睜開眼梨撞,長吁一口氣:“原來是場噩夢啊……” “哼雹洗!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起卧波,我...
    開封第一講書人閱讀 37,773評論 0 268
  • 序言:老撾萬榮一對情侶失蹤时肿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后港粱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體螃成,經(jīng)...
    沈念sama閱讀 44,220評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,550評論 2 327
  • 正文 我和宋清朗相戀三年查坪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了寸宏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,697評論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡偿曙,死狀恐怖氮凝,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情望忆,我是刑警寧澤罩阵,帶...
    沈念sama閱讀 34,360評論 4 332
  • 正文 年R本政府宣布竿秆,位于F島的核電站,受9級特大地震影響稿壁,放射性物質(zhì)發(fā)生泄漏幽钢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,002評論 3 315
  • 文/蒙蒙 一常摧、第九天 我趴在偏房一處隱蔽的房頂上張望搅吁。 院中可真熱鬧,春花似錦落午、人聲如沸谎懦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,782評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽界拦。三九已至,卻和暖如春梗劫,著一層夾襖步出監(jiān)牢的瞬間享甸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,010評論 1 266
  • 我被黑心中介騙來泰國打工梳侨, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蛉威,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,433評論 2 360
  • 正文 我出身青樓走哺,卻偏偏與公主長得像蚯嫌,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子丙躏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,587評論 2 350

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