Vous êtes sur la page 1sur 5

# Student's Information

students.name<-
c("Anirban","Chandrajit","Shrikant","Sanket","Winston","Piyush","Akash","Pratap","Randhir","Pawan","
Abhijit","Himanshu","Harsh","Nitin","Ankit","Praanshu","Devki","Rohit","Sunita","Tanvir","Udit","Varun
","James","Gargi","Babloo","Pankaj","Asha","Binod","Chameli","Dinesh","Elizabeth","Farooq","Govadha
n","Hemant","Iliana","Jeth","Kabir","Somnath","Mamon","Nilanjana") # Name of students

# Age of Students taking CAT in months
students.age<-c(250:300)

# Scores in CAT Exam
students.score<-c(50:60,95:99,55:60,76:90,60,40)

# To join all the students information
students.info<-data.frame(Name=students.name,Age=students.age,Score=students.score)

# to print the data frame
print(students.info)

# To save data CSV file
write.table(students.info,file="stat_assignment.csv",sep=",",col.names=FALSE

# to get dimension
dim(students.info)

# to get the column names
colnames(students.info)

# to calculate Mode
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}

# Mean, median and mode of age
student.mean.age<-mean(students.info$Age)
student.mean.age
student.median.age<-median(students.info$Age)
student.median.age
student.mode.age<-Mode(students.info$Age)
student.mode.age
student.sd.age<-sd(students.info$Age)
student.sd.age

# Mean, median and mode of Score
student.mean.score<-mean(students.info$Score)
student.mean.score
student.median.score<-median(students.info$Score)
student.median.score
student.mode.score<-Mode(students.info$Score)
student.mode.score
student.sd.age<-sd(students.info$Score)
student.sd.age

# Histogram of Age and test Scores

png(filename="histogram_age.png")
hist(students.info$Age)
dev.off()

png(filename="histogram_score.png")
hist(students.info$Score)
dev.off()

# Box Plot of Age and test Scores

png(filename="boxplot_age.png")
boxplot(students.info$Age)
dev.off()

png(filename="boxplot_score.png")
boxplot(students.info$Score)
dev.off()

# Divide into 4 quartile on basis of Score
students.info$qScore <- cut (students.info$Score,
breaks = quantile (students.info$Score, c (0, .25, .5, .75, 1)),
include.lowest = TRUE)

students.quartile<-aggregate (students.info, list (qScore = students.info$qScore), FUN=matrix)
students.quartile

# Average age in each quartile
# To find correlation coefficient between age and test scores in each quartiles
for(i in 1:4){
temp.age<-students.quartile[i,3]
temp.score<-students.quartile[i,4]
#temp.age<-students.quartile[i,3]
print(paste("Average age of quartile ",i,":",mean(unlist(temp.age))))
print(paste("Correltion of quartile ",i,":",cor(unlist(temp.age),unlist(temp.score))))
cat('\n')
}

# Simple linear regression model of test score with the age
student.regmodel = lm(students.info$Score ~ students.info$Age)
student.regmodel.summary<-summary(student.regmodel)
print(student.regmodel.summary)

# Summary of Regression Model
out<-capture.output(student.regmodel.summary)
cat(out,file="regmodel_smry.txt",sep="\n",append=TRUE)

# Scatter Plot of Age vs Test Scores
png(filename="regrsn_plot.png")
sctr.plot<-plot(students.info$Age,students.info$Score, main="Age vs test Scores of
Students",xlab="Age",ylab="Test Score")

# regression Line
abline(lm(students.info$Score ~ students.info$Age), col="blue")

dev.off()

Vous aimerez peut-être aussi