## Sunday, September 6, 2015

### R Programming Demo

R Programming Demo

This is a short demonstration of various things in R, some from an online class I recently completed and some from learning about R in its manual.

Matrix Multiplication

> "Matrix Multiplication"
 "Matrix Multiplication"
> a<-matrix(c(1,3,4,-8),nrow=2,byrow=TRUE)
> a
[,1] [,2]
[1,]    1    3
[2,]    4   -8
> b<-matrix(c(-3,2,0,1),nrow=2,byrow=TRUE)
> b
[,1] [,2]
[1,]   -3    2
[2,]    0    1
> "Element by Element"
 "Element by Element"
> a*b
[,1] [,2]
[1,]   -3    6
[2,]    0   -8
> "Linear Algebra Multiplication: Use %*%"
 "Linear Algebra Multiplication: Use %*%"
> a%*%b
[,1] [,2]
[1,]   -3    5
[2,]  -12    0

Solving a Linear Equation

> "Solving a Linear System"
 "Solving a Linear System"
> A <- matrix(c(3,5,-5,-1,0,3,13,4,-3),nrow=3,byrow=TRUE)
> A
[,1] [,2] [,3]
[1,]    3    5   -5
[2,]   -1    0    3
[3,]   13    4   -3
> x <- matrix(c(11,-2,10),nrow=3)
> x
[,1]
[1,]   11
[2,]   -2
[3,]   10
> solve(A,x)
[,1]
[1,]  0.1707317
[2,]  1.4878049
[3,] -0.6097561

Selecting Elements from a Vector

> "Selecting Things from a List"
 "Selecting Things from a List"
> randomvect <- c(-1.597,3.638,0.055,2.606,-2.053,-4.565,-2.470,-2.990,3.002,0.747,-2.813,2.524,0.096,-4.409,0.637)
> randomvect
 -1.597  3.638  0.055  2.606 -2.053 -4.565 -2.470 -2.990  3.002  0.747 -2.813  2.524  0.096 -4.409  0.637
> "Select Every Third Element"
 "Select Every Third Element"
> randomvect[c(FALSE,FALSE,TRUE)]
  0.055 -4.565  3.002  2.524  0.637
> "Select Elements Greater Than Zero"
 "Select Elements Greater Than Zero"
> randomvect[randomvect>0]
 3.638 0.055 2.606 3.002 0.747 2.524 0.096 0.637
> "Select Elements In Between -1 and 1"
 "Select Elements In Between -1 and 1"
> randomvect[randomvect>-1 & randomvect<1]
 0.055 0.747 0.096 0.637

Basic Linear Fit

> xdata <- c(-2,-1,-0,1,2,3,4)
> ydata <- c(-0.56,-0.36,0.15,0.33,0.52,0.77,0.93)
> df <- data.frame(x = xdata, y = ydata)
> df
x     y
1 -2 -0.56
2 -1 -0.36
3  0  0.15
4  1  0.33
5  2  0.52
6  3  0.77
7  4  0.93
> fit<-lm(df\$y~df\$x)
> fit

Call:
lm(formula = df\$y ~ df\$x)

Coefficients:
(Intercept)         df\$x
0.0007143    0.2535714

> "Clear the Plot Screen"
 "Clear the Plot Screen"
> plot.new()
> "Scatterplot"
 "Scatterplot"
> plot(df\$x,df\$y,xlab="x",ylab="y",main="Demo Scatterplot")
> "Linear Plot"
 "Linear Plot"
> abline(lm(df\$y~df\$x),col="blue",lw=2)

Plot the Trigonometric Numbers

> "Plot The Trig Functions"
 "Plot The Trig Functions"
> xvect <- seq(-2*pi,2*pi,by=pi/64)
> ysin <- sin(xvect)
> plot.new()
> plot(xvect,ysin,main="y = sin x",col="blue")

> xtan <- seq(-pi/4, pi/4, by = pi/64)
> ytan <- tan(xtan)
> par(mfrow=c(2,2))
> plot(xvect, ysin, main = "y = sin x", xlab="x", ylab="y", col="blue",pch=8)
> plot(xvect, ycos, main = "y = cos x", xlab="x", ylab="y", col="red", pch=16)
> plot(xtan, ytan, main = "y = tan x", xlab="x", ylab="y", col="forestgreen", pch=20)

> x <- c(-3.5,-2.25,-1.15,0,1.15,2.25,3.5)
> y <- c(10.6,6.3,3.1,2,3.3,6.4,10.4)
> fit <- lm(y~x+I(x^2))
> cvect <- coef(fit)
> xrng <- seq(-3.5,3.5,by=.01)
> yrng <- cvect + cvect*xrng + cvect*xrng^2
> lines(xrng,yrng)

Histogram and One Variable Statistics

> scores <- c(99,97,96,95,94,29,39,90,92,92,91,86,84,54,65,72,76,77,81,82,90,33,93,92,89,88,90,98,68,71,73,75,97)
> "Minimum Score"
 "Minimum Score"
> min(scores)
 29
> "Maximum Score"
 "Maximum Score"
> max(scores)
 99
> "Average Score"
 "Average Score"
> mean(scores)
 80.24242
> "Number of Students"
 "Number of Students"
> length(scores)
 33
 "Basic Histogram"
> hist(scores)

> "10 Categories and Labels"
 "10 Categories and Labels"
> hist(scores, breaks=10, main="Student Score Breakdown", col="yellow")

> "Fiting a Distribution"
 "Fiting a Distribution"
> hist(scores, breaks=10, main="Student Score Distribution", col="gray85",prob=TRUE)
> lines(density(scores), lwd=2, col="navy")

Citation:

R Core Team (2015). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL

A personal note, thank you for your support.  My mom and I are still in recovering/coping mode.

See you next time,

Eddie

This blog is property of Edward Shore.  2015

### HP 32S II Statistical Formulas

HP 32S II Statistical Formulas Statistics Formulas The 32S II can store formulas for evaluations, including formulas involving stat variable...