講解: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)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末煤禽,一起剝皮案震驚了整個(gè)濱河市厂汗,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌呜师,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,681評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件贾节,死亡現(xiàn)場(chǎng)離奇詭異汁汗,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)栗涂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門知牌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人斤程,你說我怎么就攤上這事角寸。” “怎么了忿墅?”我有些...
    開封第一講書人閱讀 169,421評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵扁藕,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我疚脐,道長(zhǎng)亿柑,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,114評(píng)論 1 300
  • 正文 為了忘掉前任棍弄,我火速辦了婚禮望薄,結(jié)果婚禮上疟游,老公的妹妹穿的比我還像新娘。我一直安慰自己痕支,他們只是感情好颁虐,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著卧须,像睡著了一般另绩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上故慈,一...
    開封第一講書人閱讀 52,713評(píng)論 1 312
  • 那天板熊,我揣著相機(jī)與錄音,去河邊找鬼察绷。 笑死干签,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的拆撼。 我是一名探鬼主播容劳,決...
    沈念sama閱讀 41,170評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼闸度!你這毒婦竟也來了竭贩?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,116評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤莺禁,失蹤者是張志新(化名)和其女友劉穎留量,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哟冬,經(jīng)...
    沈念sama閱讀 46,651評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡楼熄,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了浩峡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片可岂。...
    茶點(diǎn)故事閱讀 40,865評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖翰灾,靈堂內(nèi)的尸體忽然破棺而出缕粹,到底是詐尸還是另有隱情,我是刑警寧澤纸淮,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布平斩,位于F島的核電站,受9級(jí)特大地震影響咽块,放射性物質(zhì)發(fā)生泄漏双戳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望飒货。 院中可真熱鬧魄衅,春花似錦、人聲如沸塘辅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽扣墩。三九已至哲银,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間呻惕,已是汗流浹背荆责。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留亚脆,地道東北人做院。 一個(gè)月前我還...
    沈念sama閱讀 49,299評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像濒持,于是被迫代替她去往敵國和親键耕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評(píng)論 2 361

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