asadoughi / stat-learning

Notes and exercise attempts for "An Introduction to Statistical Learning"
http://asadoughi.github.io/stat-learning
2.12k stars 1.62k forks source link

Suggestion for chapter 7 exercise 12 #110

Open travis945 opened 4 years ago

travis945 commented 4 years ago

set.seed(1) p=100 n=1000 max.rep=1000 x=matrix(ncol=p,nrow=n) coefi=rep(NA,p) for (i in 1:p){ x[,i]=rnorm(n) coefi[i]=rnorm(1)100 } y=x%%coefi+rnorm(n) beta=rep(0,p) error=rep(0,max.rep) for (j in 1:max.rep){ for (i in 1:p){ a=y-x%%beta+beta[i]x[,i] beta[i]=lm(a~x[,i])$coef[2] } error[j]=sum((y-x%*%beta)^2) } error plot(1:max.rep,error,ylim=c(0,1)) plot(1:10,error[1:10])