Thursday, January 15, 2009

STAT 295 1/15/09

New charts for Tuesday: Simple Examples

Assignment (due Thursday, 1/22):

Read Chapter 1 of the book; do problems 4 and 5 on pp. 16-17

In the lecture, Jeff talked about a number of useful features of R:

Statements like y>3 are logical, and if y is a vector (or even if a is a vector) then you will get a vector of true-false values. Then, you can use z[y>3] to select out only those components of the vector z that satisfy the condition y>3, component-by-component. The resulting vector will in general be shorter than the original z vector, since it will only contain those elements less than the corresponding element of y.

Jeff made a connection between data frames and other data structures including those from SAS. I know nothing about SAS. But the basic idea here is that you can have a line or row in a data frame that represents an observation, and the individual entries may have different types. I.e., they do not all have to be numbers (as in a matrix), the first could be an integer, the second could be a logical value (TRUE, FALSE), the third could be a character string ("TRUE", "FALSE"), etc.

You can attach a data frame. This allows you to access the entries in the data frame more easily (i.e., with fewer keystrokes). However, this poses many risks. For one thing, you may have several data frames. Which one will be fetched? Also, the variables are global, and generally, good software practice advises not using global variables.

Lists: This is the best way to return multiple items from a function. You can write

return(list(x,y,z))

and get an object, the components of which are x, y, z, even if x is a number, y is a matrix, and z is a list itself of other objects.

You can access the items of a list using subscripts, e.g., if b=list(x,y,z) then

b[[1]]

will return x, whatever it is (matrix, vector, number, whatever)

And, since b[[1]] is an object, if it happened to be a vector, for example, it could be sub-accessed by whatever method was appropriate, for example if it were a vector, then the 5'th component could be obtained by writing

b[[1]][5]

If you write

f=function(...){return(list(a=x, b=y, c=z))}

then you will be able to access the objects of the returned list by using the names a, b, c. So, for example, if you've called the function f that returns that list, and said w=f(..), then if you write w$b, you will get the value of y that the function computed.

Jeff pointed out that the access to the R functions that compute standard statistical distributions like normal, binomial, etc., are prefixed by 'r', 'p', 'q', 'd', depending on the use of the desired function.

So, 'rnorm' generates a vector of normally distributed random variables. 'dnorm' calculates the normal density at the requested point. 'pnorm' gives the the cumulative distribution function, and 'qnorm' is its inverse, giving the quantile function.

On control functions, we recommend using a smart editor that will allow your braces {} to be paired so as to make clear the logic of the program. Indentation control is important!

Jeff then went into the discussion of the basics of probability. He showed that if a hypothesis A predicts that we will see evidence E more probably than hypothesis B predicts that we will observe E, then when we observe E, we should think that the evidence E supports A more than it does B.

Bill went into a song-and-dance demonstration about the differences between Bayesian and frequentist interpretations of the meaning of probability. Everyone agreed that before the coin was tossed, the probability that it would be 'heads' was 0.5; but once it was tossed, the answers were all over the place. The majority opinion was something like "it is 1 or 0, but I don't know which." The minority opinion was "it is 0.5."

Then I looked at the coin, and knew what it was. I reported that I did this, but I didn't say what I saw. Nothing much changed as regards to your opinions.

Then I told you that I saw 'heads'. Some changed opinions (this is significant).

A student looked at the coin, and said that it was 'heads'. Some changed opinions (also significant).

The point of this experiment was to reveal a critical difference between Bayesian and frequentist interpretations of probability.

In the frequentist view, you cannot talk about the probability that the coin is 'heads' or 'tails' after the coin has been tossed. This view of probability cannot talk about the probability of events that have already happened, since they are fixed.

In the Bayesian view, you can talk about such probabilities, and indeed these are the probabilities that are most important. Bayesians regard the data as fixed, and the "states of nature," AKA whether the coin is 'heads' or 'tails', as the proper thing to describe by a probability distribution.

The reason is that in the Bayesian view, probability describes your willingness to believe that a particular state of nature is true. So the 'probability' is "in your head," rather than being "out there."

We can measure this by thinking about how much you would bet that a particular proposition was true. Regardless of whether the proposition is in the future (which team wins the Superbowl this year) or already determined but unknown to us (whether the 1,000,000th digit of the decimal expansion of π is '3'), you could still make a rational bet on the truth of the proposition.

Coherent betting behavior implies the axioms of probability theory. See this link for more information!

Jeff then picked up on this and presented the basic axioms of probability theory.

No comments: