-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR Introduction.Rpres
202 lines (172 loc) · 5.17 KB
/
R Introduction.Rpres
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
R Introduction
========================================================
author: Sixiang Hu
width: 1280
height: 1024
autosize: true
What is R?
========================================================
- Open source : lots of experts in different areas are contributing to this software
- Package based : lots of packages available and you can create your own.
- Free
```{r setup,echo=FALSE,message=FALSE}
#https://github.com/mages/googleVis_KoelnR_May_2014/blob/master/googleVis_update.Rpres
options(width=120)
opts_chunk$set(out.width="1000px", fig.align="center")
library(googleVis)
options(gvis.plot.tag="chart")
library(ggplot2)
library(ggthemes)
library(forecast)
library(xts)
library(fractal)
```
Getting Started
========================================================
* Download R 3.1.2 (Pumpkin Helmet):
+ http://cran.r-project.org/
* Download RStudio (Optional):
+ http://www.rstudio.com/products/rstudio/download/
+ To get a beautiful user interface
+ Easier maintains
Basic Assignment and Operations
========================================================
* Arithmetic Operations:
```{r}
1+2*exp(3)/sin(4)
```
* Assignment:
```{r}
a <- runif(4)
a
```
* Use help in R:
```{r}
?sin
```
Variables in R
========================================================
There are several types of variables can be used in R:
integer, numerical, character, date, vector, matrix, list,
and data frame.
```{r}
a <- sample(LETTERS,5,replace=T)
a
class(a)
class(data.frame(c(a=a,b=a)))
```
Data Frame
========================================================
Data frame is the widest used data type used in R.
It can have different type of value in different columns, but
values in a column must keep the same.
```{r}
a <- c(1,2,3)
b <- c("a","b","c")
df <- data.frame(a=a,b=b)
df
df$a
df[2,2]
```
List
========================================================
List can be used to store different types of variables with
different length.
```{r}
a <- c(1,2,3)
b <- c("a","b","c")
lt <- list(a,b)
lt
lt[[2]]
```
Function
========================================================
We can create functions that can be used repetitively:
```{r}
my_square <- function(x) {x*x}
my_square(5)
```
Plot
========================================================
```{r PlotCars,fig.width=12, fig.height=12, fig.show='hold', fig.align='center'}
par(mfrow=c(2,2))
plot(cars,type="l")
hist(cars$speed)
plot(cars)
hist(cars$dist)
```
Modelling
========================================================
```{r}
model1 <- glm(mpg~gear+cyl,data=mtcars,family=Gamma)
summary(model1)
```
We can use different family and link function for GLM
modelling. Interactions, offset, splines and transformation
are all can be done within R.
For some large dataset (>100MB), it will be slow for a fit.
Some packages `bigGLM` can be used to accelerate to process.
Advanced Plot (ggplot2)
========================================================
```{r}
ggplot(data = msleep, aes(x = log(bodywt), y = sleep_total)) +
geom_point(aes(shape=vore), size=3)+
theme_wsj()
```
GoogleVis Chart
========================================================
```{r GoogleVisChart, results='asis', tidy=FALSE}
df <- data.frame(country=c("US", "GB", "BR"),val1=c(1,3,4),val2=c(23,12,32) )
Bar1 <- gvisBarChart(df, xvar="country", yvar=c("val1", "val2"))
plot(Bar1)
```
Example 1: Time Series for Market Trending
========================================================
forecast package gives automaticallal fitting to a time series developed by [Rob Hyndman](http://robjhyndman.com/)
```{r TSCode1}
MSM.raw <- read.csv("U:\\Steven\\MSMIQ\\2014 MarketPremall CREATION_DATE AGE_BAND.csv",
head=T,sep=",",stringsAsFactors=FALSE)
MSM.raw$TIME <- as.Date(MSM.raw$CREATION_DATE,"%d%b%Y")
MSM.raw <- subset(MSM.raw,CREATION_DATE != "" &
AGE_BAND == "" &
TIME>"2013-07-01" & TIME < "2014-11-30")
MSM.t<- as.xts(MSM.raw$MARKETPRICETOP5ANNUAL,MSM.raw$TIME,start=c(2013,180),end=c(2014,333),f=1/365)
obs <-length(MSM.t)
#Automatically fitting an ARIMA time series model:
MSM.t.Arima <- auto.arima(MSM.t,stationary=FALSE, seasonal=TRUE, xreg=1:obs,allowdrift=TRUE,parallel=TRUE,stepwise=FALSE)
summary(MSM.t.Arima)
```
```{r, echo=FALSE}
#We can test the stationary of a time series object:
#Staionarity Test, T: Variation over time, H0: Stationarity
#MSM.test <- stationarity(MSM.t)
#MSM.test
```
Example 1: Time Series for Market Trending (Continuous)
========================================================
```{r PlotTS,fig.width=12, fig.height=12, fig.show='hold', fig.align='center'}
MSM.t.forecast<-forecast(MSM.t.Arima,xreg=(obs+1):(obs+7))
plot(MSM.t.forecast)
```
Example 2: Chainladder for Reserving
========================================================
```{r}
```
Example 3: Decision Tree (Data Mining)
========================================================
```{r}
```
Example 4: Geographical Analysis
========================================================
```{r}
```
Further Online Tutorial
========================================================
* R Tutorial (Statistics)
+ http://www.r-tutor.com/
* CRAN R Documentations
+ http://cran.r-project.org/
* Data Camp R
+ https://www.datacamp.com/
* Kaggle
+ https://www.kaggle.com/