typical logical expressions used in programs with if statements: if(expression) { commands executed...

16
ypical logical expressions used in rograms with if statements: f(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE else { COMMANDS EXECUTED WHEN FALSE x<-3 y<-4.0 x==4.0 returns TRUE x==y returns TRUE y==y returns TRUE y==4 may return TRUE Better abs(y-4)<0.00001 When the numbers are sufficently close , then TRUE

Upload: cori-carpenter

Post on 16-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

Typical logical expressions used inprograms with if statements:

If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE

} else { COMMANDS EXECUTED WHEN FALSE }

x<-3y<-4.0

x==4.0 returns TRUEx==y returns TRUE

y==y returns TRUE

y==4 may return TRUE

Better abs(y-4)<0.00001When the numbers are sufficently close , then TRUE

Page 2: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

Typical logical expressions used in programs with if statements:

If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE

} else { COMMANDS EXECUTED WHEN FALSE }

x<0x>0x<(-1) # negative numbersmust be put in ()Logical combinations:

Logical AND: (x<y & y>3)

Logical OR: (x<y | y>3)

Logical NOT: !(x<y)

Strings can also be comparedfor equivalencemonth<-”Dec”!(month==“Jan”) returns TRUE

Note: Practice the logical operations: Be careful with vectors! Inform yourself about more operators and how they behave with vector objects! The proper use requires experience!

Page 3: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

albany_climatology_snow.R

Changes: filenameand the variable name (better called ‘object name’)

Changes: all data and variables are derived from object ‘snow’: vector ‘time’

Page 4: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

albany_climatology_snow.R

Changes: plot() function call: update the y-axis label

Changes: ‘res’ must be assigned the monthly mean snow data!it is used below for plotting in the function lines()

Page 5: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

albany_climatology_snow.R

Changes: use the year informationfrom object snow

Changes:mhelp is a new object thatstores the months data, but only for the selected years within our climatological period

Page 6: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

albany_climatology_snow.R

Changes: buffer stores the monthly mean snow dataof the climatological period

Changes: snowclimA new object to calculatethe seasonal cycle (climatological mean).

Page 7: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

albany_climatology_snow.R

Changes:plot() function call with adjusted y-axis label string

Changes: snowclimobject to calculatethe seasonal cycle (climatological mean).

Changes: lines()plots values of snowclim

Page 8: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

albany_climatology_snow.ROutlier, Erroneous data or record snow?

Page 9: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

albany_climatology_snow.R

30-yr climatology1981-2010

No snow between May-September(but in previous years May had snow!)

Page 10: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

Statistical estimates are attempts to quantify the underlying true properties of the sample population

Random samples are incomplete description of the full population

The mean of a sample is an estimate of the true mean

The variance is also only an estimate of the true variance of the population

(Any other estimates, of course too)

Page 11: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

We have seen that the sample mean is the arithmetic mean of the observationsAlbany Airport Monthly mean temperature

anomalies from the 30-yr climatology:

Sample size: 360Mean: 0 C (degree Celsius)Standard Deviation = 1.744 C

Page 12: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

We have seen that the sample mean is the arithmetic mean of the observations

New incoming data:anomaly with respect to (w.r.t.)previous estimated mean

With larger sample size each individual sample becomesless influential for updating the mean and it converges to the true mean(for Independent Identically Distributed (IID) samples)

‘online algorithm’

Page 13: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

Created with R-programscripts/online_average.R(needs data/USW00014735_tavg_mon_mean_ano.csv

Page 14: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

Histograms give an overview of the sample distribution: mean and standard deviation describe only in parts of the character of sample distributions (we will learn about the skewness and tails of distributions in this course)Albany Airport Monthly mean temperature

anomalies from the 30-yr climatology:

Sample size: 360Mean: 0 C (degree Celsius)Standard Deviation = 1.744 C

Page 15: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

We have seen that the sample mean is the arithmetic mean of the observationsAlbany Airport Monthly mean temperature

anomalies from the 30-yr climatology:

Sample size: 360Mean: 0 C (degree Celsius)Standard Deviation = 1.744 C

Page 16: Typical logical expressions used in programs with if statements: If(EXPRESSION) { COMMANDS EXECUTED WHEN TRUE } else { COMMANDS EXECUTED WHEN FALSE } x

Estimate an unknown mean of the random process (a “population mean”): we only have a sample with limited number of

observations Sample is drawn randomly from the population The larger the sample size the better the estimate That is if we repeated an experiment several times

each time with new samples of size n, thenthe variance among the estimated means willdecrease the larger the sample size n.