Search This Blog

Friday, March 16, 2012

R Programming: part#1


1. Create a vector of the positive odd integers less than 100.
2. Remove the values greater than 60 and less than 80.
3. Find mean and variance of the remaining set values.















vector=function(n)
{
    x=seq(1,n,by=2);
    #y=x[x<60];
    y=0;
    m=length(x);
    c=1;
    for(i in 1:m)
    {
        if(x[i]<60)
        {
            y[c]=x[i];
            c=c+1;
        }   
    }

    for(i in 1:m)
    {
        if(x[i]>80)
        {
            y[c]=x[i];
            c=c+1;
        }   
    }

    mean=mean(y);
    variance=var(y);
    ##return(mean);
    ##return(variance);
    result=list(y,mean,variance)
    return(result);
   
}
      ===============================================================  

4. Construct two matrices and find its product.





mult=function(n)
{
    print("[[1]] is first Matrix")
    A=array(c(1:100),dim=c(n,n));
    print("[[2]] is second matrix")
    B=array(c(101:200),dim=c(n,n));
    print("[[3]] Element by element multiplication")
    ETE=A*B;
    print("[[4]] Multiplication of Matrices")
    MP=A%*%B;
    result=list(A,B,ETE,MP);
    return(result);
       
}

=======================================================================


5. Consider the daily stock return of the Citigroup (tick symbol C) and the Standard
and Poor’s 500 Composite index from January 2001 to December 2008. The data are
simple returns and in the file d-csp0108.txt (three columns with date, C-rtn, SP-rtn).
(a) Load the above data set.
(b) Find column and row mean vectors.
(c) Make a scatterplot and histogram of C-rtn and SP-rtn. Try to fit some suitable
distribution separately for C-rtn and SP-rtn data.


citigroup=function()
{
    data_set=read.table("d-csp0108.txt",header=TRUE);
    date=data_set[1];
    C=data_set[2];
    SP=data_set[3];
   
    matrix_C_SP=data_set[2:3];
    mean_matrix_data_set=mean(data_set);
    rowwise_mean_matrix_data_set=mean(data_set,1);
    columnwise_mean_matrix_data_set=mean(data_set,2);
    mean_C=mean(C);
    mean_SP=mean(SP);
    var_C=var(C);
    var_SP=var(SP);
    tC=((C-mean_C)/(sqrt(var_C)))^3;
    tSP=((SP-mean_SP)/(sqrt(var_SP)))^3;
    skewness_C=mean(tC);
    skewness_SP=mean(tSP);

    ttC=((C-mean_C)/(sqrt(var_C)))^4;
    ttSP=((SP-mean_SP)/(sqrt(var_SP)))^4;
    kurtosis_C=mean(ttC);
    kurtosis_SP=mean(ttSP);

    ek_C=kurtosis_C-3
    ek_SP=kurtosis_SP-3

    c_rtn=t(C);
    sp_rtn=t(SP);
    date=t(date);
    x=seq(-.2,.2,length=200);
    y1=dnorm(x,mean=mean_C,sd=sqrt(var_C));
    y2=dnorm(x,mean=mean_SP,sd=sqrt(var_SP));
    # 4 figures arranged in 2 rows and 2 columns
    attach(mtcars)
    par(mfrow=c(2,2))
    plot(date,c_rtn, main=" Scatterplot of Date vs. c-rtn ")
    plot(date,sp_rtn, main="Scatterplot of date vs. sp-rtn")
    hist(c_rtn, main="Histogram of c_rtn")
    par(new=T)
    plot(x,y1,type="l",lwd=2,col="red",xlim=c(-0.1,0.1),ylim=c(0,12))
    hist(sp_rtn, main="Histogram of sp_rtn")
    par(new=T)
    plot(x,y2,type="l",lwd=2,col="red",xlim=c(-0.1,0.1),ylim=c(0,12))
C=t(C)
SP=t(SP)
library(MASS)
        fitdistr(C, "normal")

library(MASS)
        fitdistr(SP, "normal")
   
   
    result=list(mean_matrix_data_set,rowwise_mean_matrix_data_set,columnwise_mean_matrix_data_set,mean_C,mean_SP,var_C,var_SP,skewness_C,skewness_SP,kurtosis_C,kurtosis_SP,ek_C,ek_SP)
    return(result)
}

=========================================================================

6. Load the package MASS and data “JohnsonJohnson”. Extract the data corresponding
to first quarter earnings over different years and plot it.

# 2 figures arranged in 2 rows and 2 columns
    attach(mtcars)
    par(mfrow=c(2,2))
require(stats); require(graphics)
     JJ <- log10(JohnsonJohnson)
     plot(JJ)
    
    (fit <- StructTS(JJ, type="BSM"))
     tsdiag(fit)
    sm <- tsSmooth(fit)
    plot(cbind(JJ, sm[, 1], sm[, 3]-0.5), plot.type = "single", col = c("black", "green", "blue"))
    abline(h = -0.5, col = "grey60")
    
    monthplot(fit)

    JJ=log10(JohnsonJohnson);
    l=length(JJ);
    Qtr1=1:21;
    m=21;
    c=2;
    for(i in 1:20)
    {
   
   
        Qtr1[c]=JJ[i*4+1];
        c=c+1;
   
    }
    Qtr1[1]=JJ[1];
    #date=seq(1960,1980,by=1)
    #plot(date,Qtr1)

    plot(JJ,main="Plot of JJ vs. Years")
    #Qtr1=JJ[1:21];
    Time=seq(1960,1980,by=1)
    plot(Time,Qtr1,main="Plot of first quarter earnings over different years")











No comments:

Post a Comment

Thank you