Sei sulla pagina 1di 9

Table of Contents

Objects................................................................................................................. 2

Factor................................................................................................................... 2

Matrix operation................................................................................................... 2

Read statements.................................................................................................. 2

Sequence............................................................................................................. 2

Remove missing values........................................................................................ 3

LETTERS............................................................................................................... 3

FUNCTIONS.......................................................................................................... 4

Methods............................................................................................................... 4

10

Plots.................................................................................................................. 4

11

Help.................................................................................................................. 4

12

Relative frequency............................................................................................ 4

13

Reading data from internet...............................................................................4

14

Bar plot............................................................................................................. 4

15

Storing data in Timeseries................................................................................ 5

16

Mean by class................................................................................................... 5

17

Change environment......................................................................................... 5

18

Install packages................................................................................................ 6

19

Forecast using Exponential smoothing-............................................................6

20

Set working directory........................................................................................ 7

21

Decomposition and smoothing of curves..........................................................7

22

HANDLING NA values........................................................................................ 8

1 Objects
Vector same class of objects
List different class of objects
Class function provides the class name
Class(x)
Matrix vectors with a dimension attribute; created columnwise
Factors stores categorical data

2 Factor
X <- factor(c(yes,no))
X <- factor(c(yes,no)), levels = c(yes,no)

3 Matrix operation
Dim(vector name) <- c(rowno,colon)
Cbinding column binding, row binding
Cbind(x,y) or rbind(x,y)

4 Read.table
To automatically identify the class and set read.table
Initial <- read.table(foo.txt, nrows = 100)
Classes <- sapply(initial,class)
Taball <- read.table(foo.txt,colClasses =Classes)

5 Sequence
> seq(from = 1, to = 5)
[1] 1 2 3 4 5

> seq(from = 2, by = -0.1, length.out = 4)


[1] 2.0 1.9 1.8 1.7

> 1:5
[1] 1 2 3 4 5

6 Dataframes
Stores tabular data
Dataframe are similar to matrix but can store different class of objects
Row.names provides a special attribute
Read.table() or read.csv() to create dataframe
Dataframe can be converted to matrix by data.matrix()
X<- data.frame(foo = 1:4,bar = c(T,T,F,F)

7 missing values
X<- c(1,2,NA)
Bad = is.na(x)
X[!bad]

Is.na test missing value


Is.nan - testing missing nan
NAN value is one type of NA, but not the converse

>x<c(1,2,NA,4,NA,5)
>y<c("a","b",NA,"d",NA,"f")
>good<complete.cases(x,y)
>good
[1]TRUETRUEFALSETRUEFALSETRUE
>x[good]
[1]1245
>y[good]
[1]"a""b""d""f"

>airquality[1:6,]
OzoneSolar.RWindTempMonthDay
1411907.46751
2361188.07252
31214912.67453
41831311.56254
5NANA14.35655
628NA14.96656
>good<complete.cases(airquality)
>airquality[good,][1:6,]
OzoneSolar.RWindTempMonthDay
1411907.46751
2361188.07252
31214912.67453
41831311.56254
7232998.66557

8 LETTERS
> LETTERS[1:5]
[1] "A" "B" "C" "D" "E"
> letters[-(6:24)]
[1] "a" "b" "c" "d" "e" "y" "z"

9 FUNCTIONS
SUM, LENGTH,MIN,MEAN,SD,

10Methods
> methods(rev)
[1] rev.default rev.dendrogram*

11Plots
scatterplotMatrix(wine[2:6])
plot(wine$V4, wine$V5)
text(wine$V4, wine$V5, wine$V1, cex=0.7, pos=4, col="red")

time series plot

plot.ts(<<timeseries name>>)

12Help
?<typefunction>

13Relative frequency
Tbl <- table(state.division)
Tbl/Sum(Tbl)

14Reading data from internet


wine <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data",
sep=",")

15Bar plot
barplot(table(state.region), cex.names = 0.5)

16Storing data in Timeseries


birthstimeseries <- ts(births, frequency=12, start=c(1946,1))
kingstimeseries <- ts(kings)

17Mean by class
x <- tapply(salary, list(gender = gender), mean)

by(salary, gender, mean, na.rm = TRUE)

mean(salary[gender == Male])
x[which(x == max(x))]

18Change environment

. Outside of R, go into Control Panel | System | Advanced system


settings | Environment variables.
2. Under "User variables for <you>", click on New. Set Variable name to
R_LIBS. Set variable value to "C:/Users/K/Documents/Work/RLib/".
(Leave off the quotes. They probably won't hurt, but I haven't tested
that...)
3. Click OK enough time to save the changes.

19Install packages
Install.packages(package name)

20Forecast using Exponential smoothingTo make forecasts using simple exponential smoothing in R, we can fit a simple exponential
smoothing predictive model using the HoltWinters() function in R. To use HoltWinters() for
simple exponential smoothing, we need to set the parameters beta=FALSE and
gamma=FALSE in the HoltWinters() function (the beta and gamma parameters are used for
Holts exponential smoothing, or Holt-Winters exponential smoothing, as described below).
The HoltWinters() function returns a list variable, that contains several named elements.
For example, to use simple exponential smoothing to make forecasts for the time series of
annual rainfall in
London, we type:

> rainseriesforecasts <- HoltWinters(rainseries, beta=FALSE, gamma=FALSE)


> rainseriesforecasts
Smoothing parameters:
alpha: 0.02412151
beta : FALSE
gamma: FALSE
Coefficients:
[,1]
a 24.67819

Holtwinters listed variables


1. Fitted
2. SSE sum of squared errors
We can make forecasts for further time points by using the forecast.HoltWinters() function
in the R forecast package. To use the forecast.HoltWinters() function, we first need to
install the forecast R package (for instructions on how to install an R package, see How to
install an R
package).

When using the forecast.HoltWinters() function, as its first argument (input), you pass it the
predictive model that you have already fitted using the HoltWinters() function. For example,
in the case of the rainfall time series, we stored the predictive model made using
HoltWinters() in the variable rainseriesforecasts. You specify how many further time points
you want to make forecasts for by using the h parameter in forecast.HoltWinters(). For
example, to make a forecast of rainfall for the years 1814-1820 (8 more years) using
forecast.HoltWinters(), we type:
> rainseriesforecasts2 <- forecast.HoltWinters(rainseriesforecasts, h=8)
> rainseriesforecasts2
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
1913 24.67819 19.17493 30.18145 16.26169 33.09470
1914 24.67819 19.17333 30.18305 16.25924 33.09715
1915 24.67819 19.17173 30.18465 16.25679 33.09960
1916 24.67819 19.17013 30.18625 16.25434 33.10204
1917 24.67819 19.16853 30.18785 16.25190 33.10449
1918 24.67819 19.16694 30.18945 16.24945 33.10694
1919 24.67819 19.16534 30.19105 16.24701 33.10938
1920 24.67819 19.16374 30.19265 16.24456 33.11182

To plot the predictions made by forecast.HoltWinters(), we can use the plot.forecast()


function:
> plot.forecast(rainseriesforecasts2)

2.5.

21Set working directory


# Set the working directory

setwd("C:/Documents and Settings/Data")


getwd() provides the existing working directory

22Decomposition and smoothing of curves


The SMA() function in the TTR R package can be used to smooth time series data using a
simple moving average.
kingstimeseriesSMA3 <- SMA(kingstimeseries,n=3)
plot.ts(kingstimeseriesSMA3)

To estimate the trend, seasonal and irregular components of this time series, we type:
> birthstimeseriescomponents <- decompose(birthstimeseries)

23HANDLING NA values

Potrebbero piacerti anche