--------------- # Aufgabe 1 # --------------- # 1a) sx5ewithcomp = read.table("C:/Users/detlef/HSRM/Vorlesungen/WS201617/Oekonometrie/SX5EwithComp.txt",header=TRUE,sep=";") head(sx5ewithcomp) tail(sx5ewithcomp) names(sx5ewithcomp) # let's take the last 4 years of data, 2011 - 2014: sx5ewithcomp[1565,] sx5ewithcomp[1566,] sx5ewithcomp= sx5ewithcomp[-(1:1565),] head(sx5ewithcomp) tail(sx5ewithcomp) dim(sx5ewithcomp) summary(sx5ewithcomp) # remove column 47 (has 367 NA's): sx5ewithcomp = sx5ewithcomp[,-47] # remove all rows with NA's: sx5ewithcomp= na.omit(sx5ewithcomp) dim(sx5ewithcomp) # 1b) sx5e = sx5ewithcomp[,1] uls = sx5ewithcomp[,2:50] class(uls) # uls is dataframe uls = as.matrix(uls) class(uls) # now is matrix res = lm(sx5e ~ -1 + uls) summary(res) plot(sx5e,type="l") lines(res\$fit,col=2) # 1c) testfit = res\$coef[1]*uls[,1] for(k in 2:49) { testfit = testfit + res\$coef[k]*uls[,k] } plot(sx5e,type="l") lines(res\$fit,col=2) lines(testfit,col=3) # 1d) validuls = rep(TRUE,49) namesuls = colnames(uls) namesuls for(iter in 1:48) { pj = rep(0,49) for(i in 1:49) { # use only the remaining valid underlyings: if(validuls[i]==TRUE) { y = uls[,i] # remove this y from the others: uls_ohne_i = validuls uls_ohne_i[i] = FALSE X = uls[,uls_ohne_i] #res = lm(y ~ X) res = lm(y ~ -1 + X) pj[i] = sum(res\$fit^2)/sum(y^2) } } imax = which.max(pj) validuls[imax] = FALSE #print(pj) #print( paste("underlying",namesuls[imax],"has been removed.") ) #res = lm( sx5e ~ uls[,validuls] ) res = lm( sx5e ~ -1 + uls[,validuls] ) #plot(sx5e,type="l") #lines(res\$fit,col=2) #print(summary(res)) #readline("press enter to remove next underlying..") lambdas = res\$coef testvorzeichen = ifelse(lambdas<0,1,0) print(sum(testvorzeichen)) print(res\$coeff) # stop if all lambdas are positive: if( sum(testvorzeichen)==0 ) { plot(sx5e,type="l") lines(res\$fit,col=2) print(summary(res)) break # leave the loop } }