-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISEN_613_Homework_01_Script File_Manikonda Kaushik.R
72 lines (54 loc) · 2.94 KB
/
ISEN_613_Homework_01_Script File_Manikonda Kaushik.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#displays any existing objects in the workspace
ls()
#Step to check which liabraries are loaded
search()
#Removes all existing objects and variable assignments.
#Will be commented out in the final homework to avoid repetition during future executions
rm(list=ls())
#output is directed to a separate file while still appearing in the console.
#Includes the full path to show where the output file is being written.
sink("C:/Users/Kaushik/Desktop/TAMU Semesters & Courses/Fall_2020/ISEN_613 ENGR Data Analysis/Homeworks/Homework_01/HW01 Output File",split=TRUE)
pdf("C:/Users/Kaushik/Desktop/TAMU Semesters & Courses/Fall_2020/ISEN_613 ENGR Data Analysis/Homeworks/Homework_01/HW01_Output_File.pdf")
#1. Data generation and matrix indexing
#(1) Generate a vector with 25 elements and each element
#independently follows a normal distribution (with mean =0 and sd=1);
a1<-rnorm(25,mean=0, sd=1)
a1
#(2) Reshape this vector into a 5 by 5 matrix in two ways (arranged by row and column);
a2<-matrix(a1,nrow=5,byrow=FALSE)
a2
a3<-matrix(a1,nrow=5,byrow=TRUE)
a3
#(3) Similarly, generate another vector with 100 elements and plot its histogram.
a4<-rnorm(100,mean=0, sd=1)
hist(a4,freq=TRUE)
hist(a4,freq=FALSE)
#(4) Explain the plots in your own words.
#First one is a histogram of frequencies of each element of the vector
#Second one is a histogram of densities for the vector elements
#Since they were generated using rnorm, the histogram forms a normal distribution.
#2. Upload the Auto data set, which is in the ISLR library.
#Understand information about this data set by either ways
#we introduced in class (like “?Auto” and names(Auto))
library(ISLR)
names(Auto)
?Auto
#3. Make a scatterplot between any two of the following variables
#(try to plot all scatterplots in one figure; hint: use pairs() command):
#“mpg”, “displacement”, “horsepower”, “weight”, “acceleration”. By observing
#the plots, do you think the two variables in each scatterplot are correlated? If so, how?
attach(Auto)
plot(mpg,horsepower,col="blue",pch=4,ylab="Horsepower",xlab="Miles per Gallon")
plot(mpg,displacement, col="red",pch=2,ylab="Displacement",xlab="Miles per Gallon")
plot(mpg,weight, col="green",pch=3,ylab="Weight",xlab="Miles per Gallon")
plot(mpg,acceleration,col="magenta",pch=6,ylab="Acceleration",xlab="Miles per Gallon")
#4. Draw a line on the scatterplot of mpg vs. horsepower to represent relationship between
#the two variables. (Hint: Search for “R Plot Line in Scatterplot)
plot(mpg,horsepower,col="blue",pch=4,ylab="Horsepower",xlab="Miles per Gallon")
abline(lm(horsepower~mpg), col="Red")
lm(horsepower~mpg,Auto)
#5. Is there a better way to represent their relationship rather than the linear model
#you just drew? (No need to use mathematical formula. Just draw something on the figure)
#Yes, an exponential decline might be a better fit for this dataset than the linear
#model I drew.
graphics.off()