# Exhibit 1.1 on page 2. win.graph(width=4.875,height=2.5,pointsize=8) data(larain) plot(larain,ylab='Inches',xlab='Year',type='o') # Exhibit 1.2 on page 2. win.graph(width=3,height=3,pointsize=8) plot(y=larain,x=zlag(larain),ylab='Inches', xlab='Previous Year Inches') # Exhibit 1.3 on page 3. data(color) plot(color,ylab='Color Property',xlab='Batch',type='o') plot(color, color, ylab='Color Property', xlab='Batch',type='o') as.vector(color) plot(as.vector(color), color, ylab='Color Property', xlab='Batch',type='o') # Exhibit 1.4 on page 4. plot(y=color,x=zlag(color),ylab='Color Property', xlab='Previous Batch Color Property') # Exhibit 1.9 on page 7. data(oilfilters) plot(oilfilters,type='l',ylab='Sales') Month=c('J','A','S','O','N','D','J','F','M','A','M','J') points(oilfilters,pch=Month) plot(oilfilters,type='l',ylab='Sales') points(y=oilfilters,x=time(oilfilters), pch=as.vector(season(oilfilters))) y=rnorm(48) plot(y, type='p', ylab='IID Normal Data') # Exhibit 2.1 on page 14. n=60 set.seed(12345) sim.random.walk = ts(cumsum(rnorm(n)),freq=1,start=1) sim.random.walk plot(sim.random.walk,type='o',ylab='Another Random Walk') # Exhibit 3.1 on page 31. data(rwalk) model1=lm(rwalk~time(rwalk)) summary(model1) # Exhibit 3.2 on page 31. plot(rwalk,type='o',ylab='y') abline(model1) #abline(a=beta0,b=beta1) abline(a=0,b=1) model1a=lm(rwalk~time(rwalk)+I(time(rwalk)^2)) summary(model1a) # Exhibit 3.3 on page 32. data(tempdub) month.=season(tempdub) sex=factor(c('M','F','M','M','F')) sex sex=factor(c('M','F','M','M','F'),levels=c('M','F')) sex table(sex) model2=lm(tempdub~month.-1) summary(model2) fitted(model2) residuals(model2) # Exhibit 3.4 on page 33. model3=lm(tempdub~month.) # intercept is automatically summary(model3) # Exhibit 3.5 on page 35. har.=harmonic(tempdub,1) model4=lm(tempdub~har.) summary(model4) M=matrix(1:6,ncol=2) M dim(M) apply(M,2,mean) # Exhibit 3.6 on page 35. plot(ts(fitted(model4),freq=12,start=c(1964,1)), ylab='Temperature',type='l', ylim=range(c(fitted(model4),tempdub))) points(tempdub) # Exhibit 3.8 on page 43. plot(y=rstudent(model3),x=as.vector(time(tempdub)), xlab='Time', ylab='Standardized Residuals',type='o') # Exhibit 3.11 on page 45. hist(rstudent(model3),xlab='Standardized Residuals') # Exhibit 3.12 on page 45. qqnorm(rstudent(model3)) # Exhibit 3.13 on page 47. acf(rstudent(model3)) shapiro.test(rstudent(model3)) runs(rstudent(model3)) library(latticeExtra) ## 1.3 Random, normal time series #1 xyplot(as.ts(rnorm(48)),ylab="Random Values",main="Random Time Series") #2 xyplot(as.ts(rnorm(48)),ylab="Random Values",main="Random Time Series") #3 xyplot(as.ts(rnorm(48)),ylab="Random Values",main="Random Time Series") #4 xyplot(as.ts(rnorm(48)),ylab="Random Values",main="Random Time Series") #By the looks of it we can tell there is no discernable pattern in the graph visualization. ## 1.4 Random, x^2 - distributed time series #1 xyplot(as.ts(rchisq(48, 2)),ylab="Chi-Squared Values",main="Chi-Squared Time Series") #2 xyplot(as.ts(rchisq(48, 2)),ylab="Chi-Squared Values",main="Chi-Squared Time Series") #3 xyplot(as.ts(rchisq(48, 2)),ylab="Chi-Squared Values",main="Chi-Squared Time Series") #4 xyplot(as.ts(rchisq(48, 2)),ylab="Chi-Squared Values",main="Chi-Squared Time Series") #The graphs appears to be random, not normal ## 1.5 t(5)- distributed, random values #1 xyplot(as.ts(rt(48, 5)),ylab="T-distributed Values",main="T-distributed Time Series") #2 xyplot(as.ts(rt(48, 5)),ylab="T-distributed Values",main="T-distributed Time Series") #3 xyplot(as.ts(rt(48, 5)),ylab="T-distributed Values",main="T-distributed Time Series") #4 xyplot(as.ts(rt(48, 5)),ylab="T-distributed Values",main="T-distributed Time Series") #It seems to be random but not normal in these graphs.
Write, Run & Share R Language code online using OneCompiler's R Language online compiler for free. It's one of the robust, feature-rich online compilers for R language, running on the latest version 3.4. Getting started with the OneCompiler's R Language compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as R
and start coding.
R is very popular for data analytics which was created by Ross Ihaka and Robert Gentleman in 1993. Many big companies like Google, Facebook, Airbnb etc uses this language for data analytics. R is good for software developers, statisticians and data miners.
Data type | Description | Usage |
---|---|---|
Numeric | To represent decimal values | x=1.84 |
Integer | To represent integer values, L tells to store the value as integer | x=10L |
Complex | To represent complex values | x = 10+2i |
Logical | To represent boolean values, true or false | x = TRUE |
Character | To represent string values | x <- "One compiler" |
raw | Holds raw bytes |
Variables can be assigned using any of the leftward, rightward or equal to operator. You can print the variables using either print or cat functions.
var-name = value
var-name <- value
value -> var-name
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
#code
}
if(conditional-expression){
#code if condition is true
} else {
#code if condition is false
}
if(condition-expression1) {
#code if above condition is true
} elseif(condition-expression2){
#code if above condition is true
}
elseif(condition-expression3) {
#code if above condition is true
}
...
else {
#code if all the conditions are false
}
Switch is used to execute one set of statement from multiple conditions.
switch(expression, case-1, case-2, case-3....)
For loop is used to iterate a set of statements based on a condition.
for (value in vector) {
# code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
# code
}
Repeat is used tyo iterate a set of statements with out any condition. You can write a user-defined condition to exit from the loop using IF
.
repeat {
#code
if(condition-expression) {
break
}
}
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
func-name <- function(parameter_1, parameter_2, ...) {
#code for function body
}
function_name (parameters)
Vector is a basic data strucre where sequence of data values share same data type.
For example, the below statement assigns 1 to 10 values to x.
You can also use se() function to create vectors.
x <- 1:10
#using seq() function
x <- seq(1, 10, by=2)
the above statement prints the output as [1] 1 3 5 7 9
.