Lineární regrese
Jednorozměrná lineární regrese předpokládá, že výstupní prom. Y lze přibližně modelovat lineární kombinací vstupů X, tj. náš odhad y spočteme jako: kde koeficienty β spočteme z trénovacích dat. Trénovací data: Reziduum příkladu je def.:
Výpočet koeficientů lin. reg. Hledáme které minimalizují součet čtv. residuí: Derivaci položíme rovnou nule a dostaneme: kde pruh značí průměr:
Lineárně regresní model: Vrstevnice RSS v závislosti na odhadech β:
Odhad přesnosti modelu Červená: pravda, modrá: model z černých dat, světle modré: modely z jiných vzorků dat.
Standardní chyba SE Standardní chyba průměru jakožto odhadu μ pro regresní model: Platí za předpokladu nekorelovaných reziduí, pro β1 menší při větším rozptylu x. jinak jen odhad v praxi se používá. Rozptyl odhadujeme standardní chyba reziduí
Korelovaná pozorování (rezidua) např. u časové řady zpravidla podhodnocuje odhad chyby.
Rezidua nerovnoměrně nelinearita
Intervaly věrohodnosti pravdivý koef. β1 leží na 95% cca. v intevalu (gauss. chyba, studentovo rozložení) obdobně β0 v.
Testování (ne)závislosti, p-value nulová hypotéza: alternativní hypotéza: t-statistika: porovnáme s tabelovaným t-rozložením pro n-2 stupně volnosti. p-value pravděpodobnost pozorování t či více při náhodné β1 =0. Typicky při p-value<5% zamítáme nulovou hypotézu.
Správnost modelu (na tr. datech) Standardní chyba reziduí jednotky Y R 2 mezi 0 a 1 v jednorozměrném X odpovídá korelaci F-statistika
Vícerozměrná lineární regrese Model: p počet vstupních proměnných minimalizací RSS dostaneme koeficienty β. jednorozměrná: Je inzerce v novinách (dle modelu) důležitá?
Kolinearita v extrému vede k neinvertibilitě
Jen info korelace proměnných pozn. 2: Velký počet sloupců nejspíš některý i náhodou korelovaný, dobrou F statistiku atd. výběr proměnných: za chvíli.
Kvalitativní (diskrétní) proměnné Kódujeme 0/1, vícehodnotové pro každou(-1) hodnotu zvlášť. Př. národnost
Různý sklon pro třídy LR nezjistí
Nelineární modely příliš mnoho kombinací co zkoušet, pokud víte co, určitě přidejte log, exp, součin,... budou časem (resp. všechny ostatní) splajny po částech polynomiální funkce SVM chytře ve větší mocnině polynomu (nejen) basické funkce, stromy po částech nějaké stacking - uvnitř naučené modely a další.
(Ne)lineání model
Nekonstantní rozptyl reziduí log transformace, vážené nejmenší čtverce
Outliers (odlehlá pozorování) Chyba v datech nebo chybějící prediktor?
High leverage vzdálená X leverage statistics: diagonála H=X(XTX)-1XT. Jednorozměrně:
Why Linear Model Regularization? Linear models are simple, BUT consider p>>n, we have more features than data records we can (often) learn model with 0 training error even for independent features! it is overfitted model. Less features in the model may lead to smaller test error. We add constrains or a penalty on coefficients. Model with fewer features is more interpretable.22
Selection, Regularization Methods Subset Selection Shrinkage (reguralization): evaluate all subsets and select the best model (CV) a penalty on coefficients size shrunks them towards zero Dimension Reduction: from p dimension select M-dimensional subspace, M<p. fit a linear model in this M-dim. subspace. 23
Best Subset Selection Null model for( k in 1:p). fit select the one with smallest RSS, or equiv. largest R2 predicts models with exactly k predictors denote it Select a single best model from among using crossvalidation, AIC, BIC or adjusted R2. 24
Best Subset Selection tractable up to p=30,40. Simillarly, for logistic regression with deviance as error measure instead of RSS, again, CV for model 'size' selection. 25
Forward Stepwise Selection Null model for( k in 0:(p-1)) predicts. consider (p-k) adding one predictor to select the one with smallest RSS, or equiv. largest R2 denote it Select a single best model from among using crossvalidation, AIC, BIC or adjusted R2. 26
Backward Stepwise Selection Full model for( k in (p-1):0) with p predictors (standard LR). consider (k+1) models removing one predictor from select the one with smallest RSS, or equiv. largest R2 denote it Select a single best model from among using crossvalidation, AIC, BIC or adjusted R2. 27
Linear Model Selection and Regularization especially usefull in high dimensions p>>100. Full evaluation or heuristic Crossvalidation and/or analytical criteria Penalization Curse of dimensionality. 28
PCR, PLS PCR Principal component regression select direction corresponding to largest eigenvalues for these directions, regression coeff. are fitted. For size=p equivalent with linear regression. Partial least squares considers Y for selection calculates regression coefficients weight features and calculate eigenvalues select the first direction of PLS, other direction simillar, orthogonal to the first. 29
Hybrid Approaches go Forward, any time try to eliminate useless predictor. Each algorithm may provide different subset for a given size k (except 0 and p ;-) None of these has to be optimal with respect to mean test error. 30
Choosing the Optimal Model Two main approaches: Analytical criteria, adjustment to the training error to reduce overfitting ('penalty') should not be used for p>>n! Direct estimate of test error, either validation set or cross-validation approach. 31
Analytical Criteria Mallow 'in sample error estimate' Akaike: (more general, proportional to Cp here) Bayesian Information Criterion: 2 Adjusted R : equiv. minimize 32
Example 33
Validation and Cross-Validation Validation: at the beginning, exclude 1/4 of data samples from training use them for error estimation for model selection. Cross-Validation: at the beginning, split data records into k=10 folds, for k in 1:10 hide k-th fold for training use it for error estimation for model selection. Note: different runs may provide different subsets of size 3. 34
Example 35
One Standard Error Rule take the model size with the minimal CV error calculate 1 std. err. interval arround this error, select the smallest model with error inside this interval. 36
Shrinkage Methods Penalty for non-zero model parameters, no penalty for intercept. Ridge: Lasso: 37
Ridge Parameter lambda penalizes the sum of β 2. intentionally excluded from the penalty. we can center features and fix: For centered featues: for orthonormal features: Dependent on scale: standardization usefull. 38
Ridge coef. - Cancer example 39
Lasso regression the penalty is it forces some coefficients to be zero an equvivalent specification: 40
Ridge x Lasso 41
Corellated X, Parameter Shrinkage 42
Best subset, Ridge, Lasso Coefficient change for orthonormal features: 43
Example p=45, n=50, 2 predictors relate to output. 44
Linear Models for Regression Ridge, Lasso penalization PCR, PLS coordinate system change + dimension selection 45
Bias-variance decomposition Bias 'systematic error', usually caused by restricted model subspace Var variance of the estimate we wish both to be zero.
Example of Bias
Example: Lasso, Ridge Regression red: MSE green: variance black: squared Bias penalty positive
MSE: 100 observations, p differs
Penalty ~ prior model probability Ridge we assume prior probability of parameters independent, ~ then Ridge is most likely estimate (posterior mode). Bayes formula P ( β / X )= P ( X / β ) P ( β ) P(X ) P(X) constant, P ( β ) prior probability, P ( β / X ) posterior probability. P ( X / β ) likelihood, 50
Prior Probability Ridge, Laso Ridge: Normal distribution Lasso: Laplace distribution 51
Principal Component Analysis PCA 52 (vlastní čísla, vlastní vektory)