Advertisement
VRonin

Tesi

Feb 22nd, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.55 KB | None | 0 0
  1. #rm(list=ls()) #uncomment to clear the enviroment each run
  2. Alternate<-function(x,y){
  3.     if (length(x)!=length(y)){
  4.         print("Error, vectors are not of the same size!")
  5.         return()
  6.     }
  7.     result<-vector(length=length(x)*2)
  8.     result<-c(c(x,y)[c(TRUE,FALSE)],c(x,y)[c(FALSE,TRUE)])
  9.     return(result)
  10. }
  11.  
  12. GetData <- function(regression){
  13.     if (class(regression)!="lm"){
  14.         print("Error! The data can be estracted only from a liner regression model")
  15.     }
  16.     dati<-summary(regression)
  17.     result<-vector(mode="double",length=(nrow(dati$coefficients)*2)+2)
  18.     names(result)<-c(
  19.             Alternate(
  20.                     paste("Regressor",1:nrow(dati$coefficients),sep="."),
  21.                     paste("P-Value",
  22.                             paste("Regressor",1:nrow(dati$coefficients),sep="."),
  23.                             sep=".")
  24.             ),
  25.             "R^2","F significance")
  26.    
  27.     for(i in 1:nrow(dati$coefficients)){
  28.         result[((i-1)*2)+1]=dati$coefficient[i,1]
  29.         result[((i-1)*2)+2]=dati$coefficient[i,4]
  30.         i<-i+2
  31.     }
  32.     result[length(result)-1]=dati$r.squared
  33.     result[length(result)]=1-pf(dati$fstatistic[1],dati$fstatistic[2],dati$fstatistic[3])
  34.     return(result)
  35. }
  36. setwd("C:\\Users\\Luca\\Desktop\\Tesi\\R")
  37. Returns<-read.table(file="Dati Daily 2008.txt",  header=TRUE)
  38. Regressors<-Returns[,13:ncol(Returns)]
  39. Returns<-Returns[,1:12]
  40. for (i in 1:ncol(Regressors)){
  41.     Risultati<-NULL
  42.     for(j in 1:ncol(Returns)){
  43.         Betas<-GetData(lm(Returns[,j]~Regressors[,i]))
  44.         Risultati<-rbind(Risultati,Betas)
  45.         rownames(Risultati)[j]=names(Returns)[j]
  46.         if (j==1){
  47.             colnames(Risultati)<-names(Betas)
  48.         }
  49.     }
  50.     write.csv(Risultati,paste(names(Regressors)[i],".csv"),na="NA")
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement