Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Thursday, October 20, 2011

The very best Bayes Explanation?




Is by the very interesting Eliezer S. Yudkowsky. Spend some time on his site. It's good to think.



Why is Bayes important?
Here's a story problem about a situation that doctors often encounter:

1% of women at age forty who participate in routine screening have breast cancer.  80% of women with breast cancer will get positive mammographies.  9.6% of women without breast cancer will also get positive mammographies.  A woman in this age group had a positive mammography in a routine screening.  What is the probability that she actually has breast cancer?


Next, suppose I told you that most doctors get the same wrong answer on this problem - usually, only around 15% of doctors get it right. Most doctors estimate the probability to be between 70% and 80%, which is wildly incorrect.


What do you think the answer is?
(see below for the answer. See Yudkowsky for why.)
















R - Cheat sheet - Matrices

Return to the R Cheat sheet index page

cbind()  - Column bind - Equal length vectors can be combined to create a matrix.

> x<-c(1,2,3)
> y<-c(4,5,6)
> x
[1] 1 2 3
> y
[1] 4 5 6
> mm<-cbind(x,y);mm
     x y
[1,] 1 4
[2,] 2 5
[3,] 3 6
>

rbind() - As above but Row binding.

> mr<-rbind(x,y);mr
  [,1] [,2] [,3]
x    1    2    3
y    4    5    6
>

t( ) - Transpose the matrix.

> mm
     x y
[1,] 1 4
[2,] 2 5
[3,] 3 6
> t(mm)
  [,1] [,2] [,3]
x    1    2    3
y    4    5    6
>

Build a matrix in one instruction.
> m<-matrix(c(1,2,3,4,5,6), nrow=2);m

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
>

 or

> m<-matrix(c(1,2,3,4,5,6), nrow=2, byrow=T);m
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
>

Matrix multiplication
m1 %*% m2 - multiply matrix m1 with matrix m2

> m1<-matrix(c(1,5,10,12), nrow=2);m1
     [,1] [,2]
[1,]    1   10
[2,]    5   12
> m2<-matrix(c(3,4,7,9), nrow=2);m2
     [,1] [,2]
[1,]    3    7
[2,]    4    9
> m1%*%m2
     [,1] [,2]
[1,]   43   97
[2,]   63  143

and using the * operator gives scalar multiplication.
> m1*m2
     [,1] [,2]
[1,]    3   70
[2,]   20  108
>


Solve the matrix
solve(m1)

> m1
     [,1] [,2]
[1,]    1   10
[2,]    5   12
>
> ms<-solve(m1)
> ms
           [,1]        [,2]
[1,] -0.3157895  0.26315789
[2,]  0.1315789 -0.02631579
>
> ms%*%m1
     [,1] [,2]
[1,]    1    0
[2,]    0    1
>


Eigenvalues and vectors

> m1
     [,1] [,2]
[1,]    1   10
[2,]    5   12
> eigen(m1)
$values
[1] 15.458236 -2.458236

$vectors
           [,1]       [,2]
[1,] -0.5688428 -0.9450825
[2,] -0.8224463  0.3268319

>






Getting elements from a matrix





diag(mm) - Diagonal

R - Cheat sheet - Vectors

Return to the R Cheat sheet main index page

Create a vector. (Note the c)
> x<-c(1,2,5,9,15)
> x
[1]  1  2  5  9 15


Create a vector with elements of consecutive integer values.
> x<-1:7
> x
[1] 1 2 3 4 5 6 7

> x<-1:10
> x<-x*2
> x
 [1]  2  4  6  8 10 12 14 16 18 20


Some commands
min(x) - minimum of the elements in x.
max(x) - maximum of the elements in x.

sort(x)
sort(x, decreasing=T)
> y<-c(5,8,2,0,2)
> sort(y, decreasing=T)
[1] 8 5 2 2 0


length(x) - length of vector
x[n] - get nth element from vector. first element is position 1, not position 0.
x[3:7] - get range of elements

> x<-1:10
> x
 [1]  1  2  3  4  5  6  7  8  9 10
> x[3:7]
[1] 3 4 5 6 7

x[-5] Get all elements from the vector except the 5th element.

x[x>5]  Get the elements whose values are greater than 5.

x > 5 Show which elements in the vector have values greater than b5.

> x
 [1]  1  2  3  4  5  6  7  8  9 10
> x> 5
 [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE


Creating sequences
seq(n)


> seq(10)
 [1]  1  2  3  4  5  6  7  8  9 10
> seq(0,1,length=10)
 [1] 0.0000000 0.1111111 0.2222222 0.3333333 0.4444444 0.5555556 0.6666667
 [8] 0.7777778 0.8888889 1.0000000
> seq(0,1,by=0.2)
[1] 0.0 0.2 0.4 0.6 0.8 1.0


R - Cheat sheet - Basic operations

Return to the R Cheat sheet main index

> # this is a comment line
>2+3
> 2 - 3 
> log(10) # Natural log
> 3^3 # raising to the power of three
> (2*3)+6 
> sqrt(81)
> exp(4) # exponent function


Complex numbers
Re(x) - the Real part of an imaginary number.
Im(x) - the imaginary component of an imaginary number.

> # assign an imaginary number to x
> x <-8+2i
> Re(x)
[1] 8
> Im(x)
[1] 2

Trig
cos(x)
sin(x)
tan(x)
acos(x)
asin(x)
atan(x)
atan2(y, x)
Angles are in radians, not degrees (90deg is π/2).






R - Cheat sheet - The HELP in R

Return to R Cheat sheet main index

help() - this will bring up help information.
help(topic) - will deliver decimation on the topic.
help.start() - will start the HTML version of the Help.

ls() - will show objects in the path.
dir() - will show files in the current directory.









R cheat sheet. Some functions and commands in R

I've decided to break this Cheat Sheet over a few blog entries to make it easier to read.
The R project for statistical computing

Link to using Help in R
Basic operations
Vectors
Matrices
Plotting
Stats
Programming
Input and Output

Why did I bother to do this, there are better resources out there.
Try:
http://www.olivialau.org/software/Rtips.pdf
http://www.amaynard.ca/computing/R_Cheatsheet.pdf





Wednesday, October 19, 2011

Install R on Mac OSX


Preamble
How to install R onto a Mac. It's very easy.

The R site is here: http://www.r-project.org/

The Mac OS FAQ can be found here: http://cran.r-project.org/bin/macosx/RMacOSX-FAQ.html
The FAQ answers questions such as, What machines does R for Max OS X run on?, How to build from source code?
There is also a section on the R.app - that is the GUI application for the Mac.

What releases of  OS X
R will run on Mac OS X version 10.2 (Jaguar) or higher.
The GUI application for the Mac requires Mac OS X 10.3 or higher (Panther).

Download and Install
To download R for the mac goto a mirror. The list is on the CRAN page.
Visit the R site is here: http://www.r-project.org/

When you have found a suitable mirror site, download the pkg file.

Run the pkg: and get to this screen and follow the installation through.



Run the R app as normal to start the application.


Job done
That's it. A 32-bit and 64-bit version will be installed.
Getting started
R can be used a simple calculator, for example:
> 2+3
> log(10)
> 3^3
> (2*3)+6
>sqrt(81)

The cheat sheet can be found here. It's useful in getting started.

R used as a simple calculator





Saturday, October 1, 2011

A bit more on Complex numbers



We know that, 
i = sqrt (-1)

and therfore, 
i^2 = -1


Also, sqrt (9) = 3

 sqrt (-9) = sqrt(-1).sqrt(9) = i.sqrt(9) = 3i
We can check this. 
(3i)^2 = 3^2.i^2 = 9.-1 = -9

So, 
(3i)^2 = -9
3i = sqrt(-9)

3i is an imaginary number.

Complex number are imaginary and real numbers together.
e.g.

6 + 3i is a complex number.

Addition of complex numbers 
Here we have two complex numbers zi and z2

zi = a + bi
z2 = c + di
we add the real parts then add the imaginary parts.

zi + z2 = (a + c) + (bi + di)
        = (a + c) + (b + d)i

Subtraction of complex numbers 
Here we have two complex numbers zi and z2
zi = a + bi
z2 = c + di
we subtract the real parts then subtract the imaginary parts.
zi - z2 = (a - c) + (bi - di)
        = (a - c) + (b - d)i

Multiplication of complex numbers 
Again we have two complex numbers zi and z2
zi = a + bi
z2 = c + di

zi . z2 = (a + bi) . (c + di)
        = a(c+di) + bi(c+di)
        = ac+adi + cbi + (bi.di)          ....... (eqn 1)
let's sort out the (bi.di)
(bi.di) = bd.i^2
we know that i^2 = -1
so, db.i^2 = -bd
Returning to where we were in eqn (1),
zi . z2 ac + adi + cbi - (bi.di)
        = ac + adi + cbi - bd
        = (ac - bd) + (adi + cbi)
        = (ac - bd) + (ad + cb).i



Division of complex numbers 
Again we have two complex numbers zi and z2
zi = a + bi
z2 = c + di

zi / z2 = (a+bi)/(c+di)

We can use the rule:
(a+b).(a-b) = a^2-b^2

The Conjugate of a complex number is a reverse of the direction of the imaginary number.
The Conjugate of (a + bi)is (a - bi)
The Conjugate is written with a bar over the top, so the conjugate of z1 is written z1 bar. 
How do I type that here? I don't know!

When we multiply an imaginary number by its conjugate we get a Real number. Here's the trick:

zi     (a+bi)   c-di   ac-adi + bci-bdi^2
--- =  ------ . ---- = -------------------
z2     (c+di)   c-di   c^2 + d^2

      ac-adi + bci-bdi^2         [remember that , i^2 = -1]
    = ------------------- 
         c^2 + d^2


      ac-adi + bci+bd
    = ----------------- 
         c^2 + d^2

       (ac+bd) + (bc-ad)i
    = --------------------- 
         c^2 + d^2

       ac+bd         bc-ad
    =  --------  +  ------- . i
       c^2+d^2      c^2+d^2


example.

1+2i    (1+2i)   2-3i   (1.2) + (1.(-3i)) + (2.2i) + (2i.(-3i))

---- =  ------ . ---- = ---------------------------------------
2+3i    (2+3i)   2-3i   (2.2) + (2.(-3i) + (3i.(2) + (3i.(-3i))

       2 - 3i + 4i - 6i^2
     = ------------------
       4 - 6i + 6i + 9i^2


        2 - 3i + 4i + 6

     =  ------------------
             4 + 9
     
        8 + i
     =  -----
         13  
     
     =  8      1
       ---  + --- i     
       13     13