Search This Blog

Friday, March 16, 2012

R Programming: part#5


CODE

# Q. (a) Check whether the data follows multivariate normal or not. Use the result
# di = (xi − x) S −1 (xi − x) ∼ χ2 where xi ∼ Nd (μ, σ 2 ) and x and S is the mean and the standard
# deviation. Identify if there is any outlier in the data. Use package R.

mnormal = function()
{

####### --------- Lybraries ------------#####
library("MASS");
require(graphics);

data_set=read.table("m-ba4c9807.txt",header=TRUE);
BA=data_set[1];
ABT=data_set[2];
MOT=data_set[3];
GM=data_set[4];
BA=t(BA);
ABT=t(ABT);
MOT=t(MOT);
GM=t(GM);
# 4 figures arranged in 2 rows and 2 columns
#attach(mtcars)
#par(mfrow=c(2,2))
# Graphical Assessment of Multivariate Normality
x = as.matrix(data_set) # n x p numeric matrix
#print(x)
center = colMeans(x) # centroid
n = nrow(x);
p = ncol(x);
cov = cov(x);
d = mahalanobis(x,center,cov) # distances
print(t(center))
print(n)
print(p)
print(t(cov))
print(d)
qqplot(qchisq(ppoints(n), df=p), d, main="QQ Plot Assessing Multivariate Normality", ylab="Mahalanobis D2")
abline(a=0,b=1)

}


# Q no. (b) Find find out the correlation matrix. Make the graphical representation.
# Extra : Write a program of your own in SAS to calculate Spearman rank correlation and
# Kendal’s tau between BA and ABT data.

corr = function()
{
####### --------- Lybraries ------------#####
library("MASS");
require(graphics);
require(corrgram);
data_set=read.table("m-ba4c9807.txt",header=TRUE);
BA=data_set[1];
ABT=data_set[2];
MOT=data_set[3];
GM=data_set[4];
BA=t(BA);
ABT=t(ABT);
MOT=t(MOT);
GM=t(GM);
# 4 figures arranged in 2 rows and 2 columns
#attach(mtcars)
#par(mfrow=c(2,2), pty="s")
# Graphical Assessment of Multivariate Normality
x = as.matrix(data_set) # n x p numeric matrix
#print(x)

corrp = cor(x, y = NULL, use = "everything", method = "pearson" );
corrk = cor(x, y = NULL, use = "everything", method = "kendall" );
corrs = cor(x, y = NULL, use = "everything", method = "spearman" );
#print(corrp);
#print(corrk);
#print(corrs);

#cor(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman"))
#par(new=TRUE)
corrgram(corrp, type='cor', order=TRUE, lower.panel=panel.shade, upper.panel=panel.pie, text.panel=panel.txt, main=" Pearson: Car Milage Data in PC2/PC1 Order");
corrgram(x, type='data', order=TRUE, lower.panel=panel.ellipse, upper.panel=panel.pts, text.panel=panel.txt, diag.panel=panel.minmax, main="Pearson: Car Milage Data in PC2/PC1 Order");
corrgram(corrp, type='cor', order=NULL, lower.panel=panel.shade, upper.panel=NULL, text.panel=panel.txt, main="Pearson: Car Milage Data (unsorted)");
corrgram(corrp, type='cor', order="OLO", lower.panel=panel.shade, upper.panel=panel.pie, text.panel=panel.txt, main="Pearson: Car Milage Data in OLO Order");

corrgram(corrk, type='cor', order=TRUE, lower.panel=panel.shade, upper.panel=panel.pie, text.panel=panel.txt, main=" Kendall: Car Milage Data in PC2/PC1 Order");
corrgram(x, type='data', order=TRUE, lower.panel=panel.ellipse, upper.panel=panel.pts, text.panel=panel.txt, diag.panel=panel.minmax, main="Kendall: Car Milage Data in PC2/PC1 Order");
corrgram(corrk, type='cor', order=NULL, lower.panel=panel.shade, upper.panel=NULL, text.panel=panel.txt, main="Kendall: Car Milage Data (unsorted)");
corrgram(corrk, type='cor', order="OLO", lower.panel=panel.shade, upper.panel=panel.pie, text.panel=panel.txt, main="Kendall: Car Milage Data in OLO Order");

corrgram(corrs, type='cor', order=TRUE, lower.panel=panel.shade, upper.panel=panel.pie, text.panel=panel.txt, main=" Spearman: Car Milage Data in PC2/PC1 Order");
corrgram(x, type='data', order=TRUE, lower.panel=panel.ellipse, upper.panel=panel.pts, text.panel=panel.txt, diag.panel=panel.minmax, main="Spearman: Car Milage Data in PC2/PC1 Order");
corrgram(corrs, type='cor', order=NULL, lower.panel=panel.shade, upper.panel=NULL, text.panel=panel.txt, main="Spearman: Car Milage Data (unsorted)");
corrgram(corrs, type='cor', order="OLO", lower.panel=panel.shade, upper.panel=panel.pie, text.panel=panel.txt, main="Spearman: Car Milage Data in OLO Order");

#corrgram(corrp, type='cor', order = "OLO", panel=panel.shade, lower.panel=panel, upper.panel=panel, diag.panel=NULL, text.panel=textPanel, label.pos=0.5, label.srt=0, cex.labels=NULL, font.labels=1, row1attop=TRUE, dir="left", gap=0, abs=FALSE)
}


# Q no. (c) Make one sample t-tests separately to check whether there is enough evidence for the
# fact that mean of the marginals are zero. Use both R and SAS. Assume tests are of size 5%.

ttest = function()
{

####### --------- Lybraries ------------#####
library("MASS");
require(graphics);

data_set=read.table("m-ba4c9807.txt",header=TRUE);
BA=data_set[1];
ABT=data_set[2];
MOT=data_set[3];
GM=data_set[4];
BA=t(BA);
ABT=t(ABT);
MOT=t(MOT);
GM=t(GM);
# 4 figures arranged in 2 rows and 2 columns
#attach(mtcars)
#par(mfrow=c(2,2))

# Graphical Assessment of Multivariate Normality
x = as.matrix(data_set) # n x p numeric matrix
#print(x)

BAt=t.test(BA,mu=0);
print(BAt);
ABTt=t.test(ABT,mu=0);
print(ABTt);
MOTt=t.test(MOT,mu=0);
print(MOTt);
GMt=t.test(GM,mu=0);
print(GMt);
}



**********************END*************************


No comments:

Post a Comment

Thank you