1.1 讀檔
Oilprice <- read.csv("oilprice1.csv")
summary(Oilprice)
## 日期 西德州.美元.桶. 杜拜.美元.桶. 布蘭特.美元.桶.
## 2000/1/10: 1 Min. : 17.45 Min. : 15.90 Min. : 16.97
## 2000/1/11: 1 1st Qu.: 38.39 1st Qu.: 34.35 1st Qu.: 38.17
## 2000/1/12: 1 Median : 59.45 Median : 57.74 Median : 60.22
## 2000/1/13: 1 Mean : 62.34 Mean : 62.24 Mean : 65.00
## 2000/1/14: 1 3rd Qu.: 85.79 3rd Qu.: 86.81 3rd Qu.: 91.04
## 2000/1/15: 1 Max. :145.49 Max. :140.70 Max. :144.52
## (Other) :4789
## Year
## Min. :2000
## 1st Qu.:2004
## Median :2009
## Mean :2009
## 3rd Qu.:2014
## Max. :2018
##
colnames(Oilprice) <- c("Date","WestTexas","Dubai","IBCON","YEAR")
str(Oilprice)
## 'data.frame': 4795 obs. of 5 variables:
## $ Date : Factor w/ 4795 levels "2000/1/10","2000/1/11",..: 4791 4790 4788 4787 4786 4785 4784 4783 4782 4781 ...
## $ WestTexas: num 69.8 70.2 69.5 68.5 68.9 ...
## $ Dubai : num 75.7 75.7 74.3 75 74.3 ...
## $ IBCON : num 77.7 77.8 77.3 75.9 76.3 ...
## $ YEAR : int 2018 2018 2018 2018 2018 2018 2018 2018 2018 2018 ...
Oilprice$YEAR <- factor(Oilprice$YEAR)
1.2 單一變數
#2000年-2018年內各地區油價
mean(Oilprice$WestTexas) #平均數
## [1] 62.34499
sd(Oilprice$WestTexas) #標準差
## [1] 27.03306
sd(Oilprice$WestTexas)^2 #變異數
## [1] 730.7862
mean(Oilprice$Dubai) #平均數
## [1] 62.2387
sd(Oilprice$Dubai) #標準差
## [1] 30.42798
sd(Oilprice$Dubai)^2 #變異數
## [1] 925.8622
mean(Oilprice$IBCON) #平均數
## [1] 64.99649
sd(Oilprice$IBCON) #標準差
## [1] 30.90746
sd(Oilprice$IBCON)^2 #變異數
## [1] 955.2711
1.3 箱型圖BoxPlot
installed.packages("ggplot2")
## Package LibPath Version Priority Depends Imports LinkingTo Suggests
## Enhances License License_is_FOSS License_restricts_use OS_type Archs
## MD5sum NeedsCompilation Built
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
#箱型圖Boxplot
#西德州從2000-2018年內油價的變化
boxplot(WestTexas ~ YEAR,
Oilprice,
main = " WestTexas's oil price",
xlab ="Year",
ylab = "Price",
col = "grey"
)

#杜拜從2000-2018年內油價的變化
boxplot(Dubai ~ YEAR,
Oilprice,
main = " Dubai's oil price",
xlab ="Year",
ylab = "Price",
col = "grey"
)

#倫敦布蘭特原油從2000-2018年內油價的變化
boxplot(IBCON ~ YEAR,
Oilprice,
main = " IBCON's oil price",
xlab ="Year",
ylab = "Price",
col = "grey"
)

#各地區油價2000-2018年箱型圖
boxplot(Oilprice$WestTexas,
main="WestTexas",
xlab="2000-2018",
ylab="Price",
col = "grey")

boxplot(Oilprice$Dubai,
main="Dubai",
xlab="2000-2018",
ylab="Price",
col = "grey")

boxplot(Oilprice$IBCON,
main="IBCON",
xlab="2000-2018",
ylab="Price",
col = "grey")
