Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #rm(list=ls()) #uncomment to clear the enviroment each run
- Alternate<-function(x,y){
- if (length(x)!=length(y)){
- print("Error, vectors are not of the same size!")
- return()
- }
- result<-vector(length=length(x)*2)
- result<-c(c(x,y)[c(TRUE,FALSE)],c(x,y)[c(FALSE,TRUE)])
- return(result)
- }
- GetData <- function(regression){
- if (class(regression)!="lm"){
- print("Error! The data can be estracted only from a liner regression model")
- }
- dati<-summary(regression)
- result<-vector(mode="double",length=(nrow(dati$coefficients)*2)+2)
- names(result)<-c(
- Alternate(
- paste("Regressor",1:nrow(dati$coefficients),sep="."),
- paste("P-Value",
- paste("Regressor",1:nrow(dati$coefficients),sep="."),
- sep=".")
- ),
- "R^2","F significance")
- for(i in 1:nrow(dati$coefficients)){
- result[((i-1)*2)+1]=dati$coefficient[i,1]
- result[((i-1)*2)+2]=dati$coefficient[i,4]
- i<-i+2
- }
- result[length(result)-1]=dati$r.squared
- result[length(result)]=1-pf(dati$fstatistic[1],dati$fstatistic[2],dati$fstatistic[3])
- return(result)
- }
- setwd("C:\\Users\\Luca\\Desktop\\Tesi\\R")
- Returns<-read.table(file="Dati Daily 2008.txt", header=TRUE)
- Regressors<-Returns[,13:ncol(Returns)]
- Returns<-Returns[,1:12]
- for (i in 1:ncol(Regressors)){
- Risultati<-NULL
- for(j in 1:ncol(Returns)){
- Betas<-GetData(lm(Returns[,j]~Regressors[,i]))
- Risultati<-rbind(Risultati,Betas)
- rownames(Risultati)[j]=names(Returns)[j]
- if (j==1){
- colnames(Risultati)<-names(Betas)
- }
- }
- write.csv(Risultati,paste(names(Regressors)[i],".csv"),na="NA")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement