statistical computing 03

Post on 05-Apr-2017

162 Views

Category:

Science

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Statistical Computing — Distribution and loop —

徐峻賢中央研究院語⾔言學研究所⼤大腦與語⾔言實驗室

• 作業⼀一:

• 請閱讀下⾯面這篇⽂文章

• https://www.r-bloggers.com/python-r-vs-spss-sas/

• 根據作者提供的訊息,寫出兩點 SPSS/SAS 與 R 的⽐比較。

• 不要寫三點以上,不要寫「⼀一個要錢、⼀一個免費」

d… returns the height of the probability density function

p… returns the cumulative density function

q… returns the inverse cumulative density function (quantiles)

r… returns randomly generated numbers

產⽣生隨機序列

PURPOSE SYNTAX EXAMPLE

RNORM Generates random numbers from normal distribution rnorm(n, mean, sd)

rnorm(1000, 3, .25)Generates 1000 numbers from a normal with mean 3 and sd=.25

DNORM Probability Density Function(PDF) dnorm(x, mean, sd)

dnorm(0, 0, .5)Gives the density (height of the PDF) of the normal with mean=0 and sd=.5. 

PNORMCumulative Distribution Function(CDF) pnorm(q, mean, sd)

pnorm(1.96, 0, 1)Gives the area under the standard normal curve to the left of 1.96, i.e. ~0.975

QNORMQuantile Function – inverse ofpnorm qnorm(p, mean, sd)

qnorm(0.975, 0, 1)Gives the value at which the CDF of the standard normal is .975, i.e. ~1.96

PURPOSE SYNTAX EXAMPLE

RT Generates random numbers from t distribution rt(n, df)

rt(1000, 30)Generates 1000 numbers from a t distribution with df = 30

DT Probability Density Function(PDF) dt(x, df)

dt(0, 30)Gives the density (height of the PDF) of the t dist. at t = 0 with df = 30

PTCumulative Distribution Function(CDF) pt(q, df, lower.tail)

pt(1.96, 30)Gives the area under the t dist. to the left of 1.96, i.e. ~0.975

QTQuantile Function – inverse ofpnorm qt(p, df, lower.tail)

qt(0.975, 30)Gives the value at which the CDF of the t dist. is .975, i.e. ~1.96

for loopprint(paste("The year is", 2010))"The year is 2010"print(paste("The year is", 2011))"The year is 2011"print(paste("The year is", 2012))"The year is 2012"print(paste("The year is", 2013))"The year is 2013"print(paste("The year is", 2014))"The year is 2014"print(paste("The year is", 2015))"The year is 2015”……

for (year in c(2010,2011,2012,2013,2014,2015)){ print(paste("The year is", year))}"The year is 2010""The year is 2011""The year is 2012""The year is 2013""The year is 2014""The year is 2015"

for (i in 2010:2015){ print(paste("The year is", i))}"The year is 2010""The year is 2011""The year is 2012""The year is 2013""The year is 2014""The year is 2015"

if statementExample: if statementx <- 5 if(x > 0){ print("Positive number") } Output[1] "Positive number"

if statementThe syntax of if statement is:if (test_expression) { statement }

if statementif...else statementThe syntax of if...else statement is:if (test_expression) { statement1 } else { statement2 }

Example of if...else statementx <- -5 if(x > 0){ print("Non-negative number") } else { print("Negative number") } Output[1] "Negative number"

⽐比較優雅的寫法:

if(x > 0) print("Non-negative number") else print("Negative number”)

x <- -5 y <- if(x > 0) 5 else 6 y [1] 6

if statementNested if...else statementWe can nest as many if...else statement as we want as follows.The syntax of nested if...else statement is:if ( test_expression1) { statement1 } else if ( test_expression2) { statement2 } else if ( test_expression3) { statement3 } else statement4

Example of nested if...elsex <- 0 if (x < 0) { print("Negative number") } else if (x > 0) { print("Positive number") } else print("Zero") Output[1] "Zero"

top related