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





Council could probably do with a test script.

1. Stand back and look at sign.
2. Assert.IsVisible(sign);
3. Assert.IsCorrectlyOrientated(sign);
4. Assert.IsEqual(SignRequested, SignSlungUpByCouncilMuppet);

http://www.bbc.co.uk/news/uk-england-manchester-15371102


A spokesman for Oldham Council said "a genuine error was made by staff".
A "genuine error".
"Was made by staff". People that go around putting up the Council's signs are indeed staff of the Coucil.



Monday, October 17, 2011

Running a marathon when you're 100.

Mr Singh said: "The secret to a long and healthy life is to be stress-free. Be grateful for everything you have, stay away from people who are negative, stay smiling and keep running."


http://www.bbc.co.uk/news/world-us-canada-15330421




He puts his stamina down to ginger curry, tea and "being happy".