Saturday, 30 March 2013

Adding Dimensions in R

Assignment 1: 

Create 3 vectors, x, y, z and choose any random values for them, ensuring they are of equal length, bind them together.Create 3 dimensional plots of the same.

Data Set Creation Commands and DataSet :

> data<-rnorm(50,mean=20,sd =10)
> x<-sample(data,10)
> y<-sample(data,10)
> z<-sample(data,10)
> t<-cbind(x,y,z)
       
                                                                                                                                                                           

Normal Plot:   plot3d(T[, 1:3])   



Colour Plot: plot3d(T[, 1:3], col = rainbow(1000))




Color Plot of spheres:  plot3d(T[, 1:3], col = rainbow(1000), type = 's')





Assignment 2:

Choose 2 random variables
Create 3 plots:
1. X-Y
2. X-Y|Z (introducing a variable z and cbind it to z and y with 5 diff categories)
3. Color code and draw the graph
4. Smooth and best fit line for the curve

Data set creation for two random variables and then introducing third variable z


> x<-rnorm(5000,mean=20,sd=10)
> y<-rnorm(5000,mean=20,sd=10)
> z1<-sample(letters,5)
> z2<-sample(z1,5000,replace=TRUE)
> z<-as.factor(z2)






Plots:
>qplot(x,y)



>qplot(x,z)

                             

Semi-transparent plot

> qplot(x,z, alpha=I(2/10))
                           


Colour plot

> qplot(x,y, color=z)

                             

Logarithmic colour plot

> qplot(log(x),log(y), color=z)
                               

Best Fit and Smooth curve using "geom"

> qplot(x,y,geom=c("path","smooth"))

                                     


> qplot(x,y,geom=c("point","smooth"))
                                   

> qplot(x,y,geom=c("boxplot","smooth"))
                                         

> qplot(x,y,geom=c("boxplot","jitter"))






























                 

Saturday, 23 March 2013

WOLFRAM/ALPHA Personal Analytics for Facebook


To get your personal analytics report of Facebook all you have to do is type “Facebook report” into the standard Wolfram|Alpha website.
1) If you’re doing this for the first time, you’ll be prompted to authenticate the Wolfram Connection app in Facebook, and then sign in to Wolfram|Alpha . And as soon as you’ve done that, Wolfram|Alpha will immediately get to work generating a personal analytics report from the data it can get about you through Facebook.
Here’s the beginning of the report I get today when I do this:


2)After this , I get my report .Reports start with my basic personal information & which places I belong to :






     

3)We can get demographic profile of our friends which tells you % of males & females as well age profile of all friends.






4)We come to know what are the words that we talk the most while we use the facebook .



6) We can also get to know the time when when we use the facebook the most .
                      
 7) We can find the pictures as well as the status update which we were like the most .





8) We can find the places where are friends are located. 



9) We can also find Relationship status of the friends on this with the help of a pie diagram.





           Note : Wolfram Alpha has a wide number application apart from this . It is helpful in various area such as statistic,corporate finance ,Investment banking etc.http://www.wolframalpha.com/




Friday, 15 March 2013

Assignment 8 : Panel Data Analysis

There are three types of models:
  •       Pooled affect model
  •       Fixed affect model
  •       Random affect model 
We will be determining which model is the best by using functions:
       pFtest : for determining between fixed and pooled
       plmtest : for determining between pooled and random
       phtest: for determining between random and fixed
The data can be loaded using the following commands:-
data(Produc , package =”plm”)
head(Produc)


Pooled Affect Model 
pool <-plm( log(pcap) ~log(hwy)+ log(water)+ log(util) + log(pc) + log(gsp) + log(emp) + log(unemp), data=Produc,model=(“pooling”),index =c(“state”,”year”))
summary(pool)



Fixed Affect Model:
fixed<-plm( log(pcap) ~log(hwy)+ log(water)+ log(util) + log(pc) + log(gsp) + log(emp) + log(unemp), data=Produc,model=(“within”),index =c(“state”,”year”))
summary(fixed)
                 
                    

Random Affect Model:
random <-plm( log(pcap) ~log(hwy)+ log(water)+ log(util) + log(pc) + log(gsp) + log(emp) + log(unemp), data=Produc,model=(“random”),index =c(“state”,”year”))
> summary(random)


Comparison
This can be done through Hypothesis testing between the models as follows:
H0: Null Hypothesis: the individual index and time based params are all zero
H1: Alternate Hypothesis: atleast one of the index and time based params is non zero
Pooled vs Fixed
Null Hypothesis: Pooled Affect Model
Alternate Hypothesis : Fixed Affect Model
Command:
 > pFtest(fixed,pool)
Result:
data:  log(pcap) ~ log(hwy) + log(water) + log(util) + log(pc) + log(gsp) + log(emp) + log(unemp)
F = 56.6361, df1 = 47, df2 = 761, p-value < 2.2e-16
alternative hypothesis: significant effects 
Since the p value is negligible so we reject the Null Hypothesis and hence Alternate hypothesis is accepted which is to accept Fixed Affect Model.




Pooled vs Random
Null Hypothesis: Pooled Affect Model
Alternate Hypothesis: Random Affect Model
Command :
> plmtest(pool)
Result:
 Lagrange Multiplier Test – (Honda)
data:  log(pcap) ~ log(hwy) + log(water) + log(util) + log(pc) + log(gsp) + log(emp) + log(unemp)
normal = 57.1686, p-value < 2.2e-16
alternative hypothesis: significant effects 
 Since the p value is negligible so we reject the Null Hypothesis and hence Alternate hypothesis is accepted which is to accept Random Affect Model.


Random vs Fixed
Null Hypothesis: No Correlation . Random Affect Model
Alternate Hypothesis: Fixed Affect Model
Command:
 > phtest(fixed,random)
Result: 
 Hausman Test
data:  log(pcap) ~ log(hwy) + log(water) + log(util) + log(pc) + log(gsp) + log(emp) + log(unemp)
chisq = 93.546, df = 7, p-value < 2.2e-16
alternative hypothesis: one model is inconsistent . 
Since the p value is negligible so we reject the Null Hypothesis and hence Alternate hypothesis is accepted which is to accept Fixed Affect Model.



Conclusion: 
So after making all the tests we come to the conclusion that Fixed Affect Model is best suited to do the panel data analysis for “Produc” data set.
Hence , we conclude that within the same id i.e. within same “state” there is no variation












Wednesday, 13 February 2013

Stationary Time Series

Assignment 1:

Create log of returns for NIFTY data from 01 Jan 2012 to 31 Jan 2013 and calculate the historical volatility

The command for determining the log of returns and historical volatility is as follows:

z<-read.csv(file.choose(), header =T)
head(z)
closingprice<-z[,5]
closingprice.ts<-ts(closingprice, frequency =252)
st<-log(closingprice.ts)
stlag<-log(lag(closingprice.ts,k=-1))
log.returns<-(st-stlag)/stlag
plot(log.returns)
T =(252) ^ 0.5
historicalvolatility<-sd(returns) * T
historicalvolatility

The plot for log of returns is as follows:



Assignment 2:

Create ACF plot for the above log of returns data and perform the adf test and comment on it

The ACF plot can be done using the below formula :
acf(log.returns)

The Plot is as follows:






The Plot suggests us that our data lies inside the confidence interval of 95% and there is maximum possibility of being stationary.

The ADF test is performed using the below formula:

adf.test(returns)

The output is as follows:


It shows that the P value is less than the significant value(0.05) and we can reject the null hypothesis.

Since the alternate hypothesis is stated as data being stationary we can conclude that data is stationary and analysis can be done.

Wednesday, 23 January 2013

Assignment3


Assignment 1(a): Using the given data of mileage-groove, Fit LM and comment on its usability:
Running the Regression


Residual Plot
As the plot is not scattered, linearity is not applicable for the given data.

Assignment 1(b): Using the given data of alpha-pluto, Fit lm and comment on its applicability:

Given data


Regression and Residual plots

As the plot is random, Linearity is applicable.


QQ Plot and QQ line

Assignment 2: Justify Null Hypothesis using ANOVA




p value=0.687

Since p-value is greater than 5%, we cannot reject the null hypothesis. 

Tuesday, 15 January 2013

Session2

Assignment1: Binding of two columns



Assignment2: Multiplying two matrix



Assignment3: Plotting regression using NSE data from 1Dec to 1Jan.


Assginment4: Generate a normal Distribution data & plot it.

The Required Code :x<-seq(-50,50,by=1)
                             > y<-dnorm(x,mean=0,sd=1)
                             > plot(x,y,type="l")