Μάθημα : ΓΡΑΜΜΙΚΑ ΜΟΝΤΕΛΑ
Κωδικός : MATH315
Εντολες R lab1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Lab 1: Introduction ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This script includes the basics of R. No former experience on programming or
# statistics is required.
# You can use R or Rstudio, whichever you prefer.
# To run current line or selection in R: Ctrl+R
# To run current line or selection in Rstudio: Ctrl+Enter
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Basic Algebra ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# You can use R as a calculator:
3 + 6
3 - 6
3 * 6
3 / 6
3 ^ 2
# Generally, it is better to assign your values into a variable, which R will
# remember for future use.
a <- 3
b <- 6
a + b
# The name of a variable should be short and clear.
year <- 2000 # good name
year_of_new_millenium <- 2000 # too long
yonm <- 2000 # short, but unclear
# Check if two variables are equal.
x <- 3
y <- 5
x == y
x != y
# See all your variables.
ls()
# Delete a variable. (rm comes from "remove")
rm(a)
ls()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Functions ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a variable
x <- 3
# Square root
sqrt(x)
# Logarithm (attention, for R this is ln, not log)
log(x)
# Exponential
exp(x)
exp(log(x))
# Absolute
abs(-5)
# Get info about a function
?sqrt
?exp
# We can create a function of our own
my_sum <- function(x, y) {
z <- x + y
z
}
my_sum(2, 3)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Vectors ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# R can also work with vectors (dianysmata) and matrices (pinakes). R views
# vectors as a column.
# Create a vector
x <- c(2, 4, 6, -8)
x
y <- c(3, -5, 7, 11)
y
# Basic algebra with vectors
x + 2
2 * y
x ^ 2
1 / y
# Create a sequence of numbers
a <- 1:20
a
b <- 15:1
b
# Create a more complex sequence
seq(0, 10, by = 0.5)
seq(0, 5, length = 30)
# Repeat a number or vector
rep(0, times = 10)
rep(c(1, 2, 3), times = 3)
rep(c(1, 2, 3), each = 10)
# Get the third value of x
x[3]
# Get the 1st and the 3rd value of y
y[c(1, 3)]
# Get x without the 2nd and 3rd value
x[-c(2, 3)]
# Keep the values of x that are less than 3
x[x < 3]
# Find the position of these elements
which(x < 3)
# Keep the values of y between 1 and 10
y[y > 1 & y < 10]
# Find out if x and y are identical
identical(x, y)
# Sum the elements of x
sum(x)
# Find the length of y
n <- length(y)
n
# Transpose (anastrofos) - The vector is now a row
t(x)
# Multiply each element of x with an element of y
x * y
# Multiply the two vectors as usual (esoteriko ginomeno)
t(x) %*% y
# Multiply x (a column) with t(y) (a row)
x %*% t(y)
# Attention!
# x*y is a vector
# t(x)%*%y is a numeric
# x%*%t(y) is a matrix
# You can simply type x%*%y instead of t(x)%*%y and R will print the same
# (esoteriko ginomeno)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Matrices ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# There are four primary ways to create a matrix in R
# 1: Use "rbind" to stack two vectors by row
A <- rbind(c(1, 2, 3), c(4, 5, 6))
A
# 2: Use "cbind" to stack two vectors by column
B <- cbind(c(1, 3), c(2, 4))
B
# 3: Use "matrix" to break a long vector into columns
C <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
C
# 4: Use "matrix" to break a long vector into rows
D <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)
D
# Index a matrix: get first column of A
A[, 1]
# Index a matrix: get first row of B
B[1, ]
# Index a matrix: get the element (1,2) of C
C[1, 2]
# Basic Algebra with the elements of a matrix
2 * A
A ^ 2
A + C
# Transpose of a matrix
t(A)
# Find the determinant (orizousa) of a matrix
det(B)
det(C) #why does this not work?
# Find out if which matrices are identical
identical(A, D)
identical(C, D)
# Matrix Multiplication
B %*% A
B %*% B
C %*% t(D)
# Eigenvalues, eigenvectors (idiotimes, idiodianysmata)
eigen(B)
# Inverse matrix (antistrofos)
solve(B)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Random Samples ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Simulate n draws from N(mu, sd), the Normal distribution with mean equal to mu
# and standard deviation equal to sd.
n <- 100
mu <- 0
sd <- 1
x <- rnorm(n, mu, sd)
mean(x) # sample mean
var(x) # sample variance
n <- 10000
mu <- 10
sd <- 3
x <- rnorm(n, mu, sd)
hist(x)
# Density function - Synarthsh Pyknothtas f(y)
y <- 2
dnorm(y, mu, sd)
# Distribution function - Synarthsh Katanomhs P(Y<=y)
pnorm(y, mu, sd)
# Simulate from the Beta distribution
n <- 10000
x <- rbeta(n, 2, 4)
hist(x)