{"word_count": 570, "character_count": 4957}
For my lectures on applied linear models, I wanted to illustrate the fact that the [latex]R^2[/latex] is never a good measure of the goodness of the model, since it's quite easy to improve it. Consider the following datasetn=100df=data.frame(matrix(rnorm(n*n),n,n))names(df)=c("Y",paste("X",1:99,sep=""))with one variable of interest [latex]y[/latex], and 99 features [latex]x_j[/latex]. All of them being (by construction) independent. And we have 100 observations... Consider here the regression on the first [latex]k[/latex] features, and compute [latex]R_k^2[/latex] of that regressionreg=function(k){  frm=paste("Y~",paste("X",1:k,collapse="+",sep=""))  model=lm(frm,data=df)  summary(model)$adj.r.squared}Let us see what's going on...plot(1:99,Vectorize(reg)(1:99))(actually, it's not exactly what we have on the graph.... we have the average obtained over 1,000 samples randomly generated, with 90% confidence bands). Oberve that [latex]\mathbb{E}[R^2_k]=k/n[/latex], i.e. if we add some pure random noise, we keep increasing the [latex]R^2[/latex] (up to 1, actually).Good news, as we've seen in the course, the adjusted [latex]R^2[/latex] - denoted [latex]\bar R^2[/latex]-might help. Observe that [latex]\mathbb{E}[\barR^2_k]=0[/latex], so, in some sense, adding features does not help here...reg=function(k){  frm=paste("Y~",paste("X",1:k,collapse="+",sep=""))  model=lm(frm,data=df)  summary(model)$r.squared}plot(1:99,Vectorize(reg)(1:99))We can actually do the same with Akaike criteria [latex]AIC_k[/latex] and Schwarz (bayesian) criteria [latex]BIC_k[/latex].reg=function(k){  frm=paste("Y~",paste("X",1:k,collapse="+",sep=""))  model=lm(frm,data=df)  AIC(model)}plot(1:99,Vectorize(reg)(1:99))For the [latex]AIC[/latex], the intitial increase makes sense : we should not prefer the model with 10 covariates, compared with nothing. The strange thing is the far right behavior : we prefer here 80 random noise features to none ! Which I find hard to interprete... For the [latex]BIC[/latex] the code is simplyreg=function(k){  frm=paste("Y~",paste("X",1:k,collapse="+",sep=""))  model=lm(frm,data=df)  BIC(model)}plot(1:99,Vectorize(reg)(1:99))and here also, we have the same pattern, where we prefer a big model with juste pure noise to nothing...A last one to conclude (or not) : what about the leave-one-out cross validation mean squared error ? More precisely, [latex display="true"]CV=\frac{1}{n}\sum_{i=1}\widehat{\varepsilon}^2_{-i}[/latex]where [latex]\widehat{\varepsilon}^2_{-i}=y_i-\widehat{y}_{-i}[/latex] where [latex]\widehat{y}_{-i}[/latex] is the predicted value obtained with the model is estimated when the [latex]i[/latex]th observation is deleted. One can prove that [latex display="true"]\widehat{\beta}_{-i}=\widehat{\beta}-(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{x}_i\hat\varepsilon_i(1-H_{i,i})^{-1}[/latex]where [latex]H[/latex] is the classical hat matrix, thus[latex display="true"]\widehat{\varepsilon}_{-i}=(1-H_{i,i})^{-1}\hat\varepsilon_i[/latex]i.e. we do note have to estimate (at each round) [latex]n[/latex] modelsreg=function(k){  frm=paste("Y~",paste("X",1:k,collapse="+",sep=""))  model=lm(frm,data=df)  h=lm.influence(model)$hat/2  mean( (residuals(model)/1-h)^2 ))}plot(1:99,Vectorize(reg)(1:99))Here, it make sense : adding noisy features yields overfit ! So the mean squared error is decreasing !That's all nice, but it might not be very realistic... Here, for my model with only one variable, I just pick one, at random.... In practice, we try to get the "best one"... So a more natural idea would be to order the variables according to their correlations with [latex]y[/latex],df=data.frame(matrix(rnorm(n*n),n,n))  df=df[,rev(order(abs(cor(df)[1,])))]  names(df)=c("Y",paste("X",1:99,sep=""))}and as before, we can plot the evolution of [latex]R^2_k[/latex] as a function of [latex]k[/latex] the number of features considered,which is increasing, with a higher slope at the beginning... For the [latex]\bar R^2_k[/latex] we might actually prefer a correlated noise to nothing (which makes sense actually). So here since we somehow chose our variables, [latex]\bar R^2_k[/latex] seems to be always positive...For the [latex]AIC_k[/latex] here also, there is an improvement. Before coming back to the original situation (with about 80 features) and here also, we observe the drop on the far right part of the graphThe [latex]BIC_k[/latex] might like the top three features, but soon, we have a deterioration.... even if here also, we have the drop at the far right (with more than 95 features... for 100 observations).Finally, observe that here again, our (leave-one-out) cross-validation has not been mesled by our noisy variables : it is always decreasing !So it seems that cross-validation techniques are more robust than the [latex]AIC[/latex] and [latex]BIC[/latex] (even if we mentioned in a previous post connexions between all those concepts) when we have a lot a noisy (non-relevent) features.