do.it<-function(n=20,p=5){ # generate some fake data X=matrix(rnorm(n*p),n,p) b=sample(c(0.5,1,1.5,2,2.5,3),size=p,replace=TRUE) y=as.vector(X %*% b + rnorm(n)) # take care of some data structure issues in R X=data.frame(X) dimnames(X)[[2]]<-paste('x',1:p,sep='') # fit a full model and a reduced model (__without last column__) obj =lm(as.formula(paste('y ~ ',paste('x',1:p, sep='',collapse='+'))),data=X) obj0=lm(as.formula(paste('y ~ ',paste('x',1:(p-1),sep='',collapse='+'))),data=X) # look at the full model cat('**********full model**********\n\n') print(round(summary(obj)$coef,4)) cat('\n') # look at the reduced model cat('**********reduced model**********\n\n') print(round(summary(obj0)$coef,4)) cat('\n') # F-test (note p-q=1 here) numer=sum(obj0$resid^2)-sum(obj$resid^2) denom=sum(obj$resid^2)/(n-p-1) cat('**********F-test**********\n\n') cat('F-statistic = \t', round(numer/denom,4), '\n') } do.it()