16
Statistical Computing — data input & browser in RStudio — 徐峻賢 中央研究院語學研究所 腦與語實驗室

Statistical computing 01

Embed Size (px)

Citation preview

Page 1: Statistical computing 01

Statistical Computing — data input & browser in RStudio —

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

Page 2: Statistical computing 01

本週學習重點•・ 認識 RStudio 軟體環境

•・ 資料類型

•・ hands-on exercise

Page 3: Statistical computing 01

(1) 執⾏行程式

程式存檔 (myRcode.R)

(3) 利⽤用source 執⾏行程式> source(“myRcode.R”)

(2) 執⾏行程式

Page 4: Statistical computing 01
Page 5: Statistical computing 01

•・ ⼯工作區存檔 File => Workspace (.RData)

•・ 歷史指令存檔File => History (. Rhistory)

•・ 改變⼯工作⽬目錄 File => Change dir…

> getwd

> setwd(“D:\\my-R”)

> dir() #查看⽬目前⽬目錄下的檔案

•・ Rnw stands for R NoWeb fi゙les. The RNW fi゙le type is primarily associated with 'Sweave'. Sweave is a tool that allows you to embed the R code for complete data analyses in latex documents.

•・ name.rdb: a concatenation of serialized objects.

•・ name.rdx: fi゙le contains an index.

•・ name.rds the serialized R objects written by saveRDS

•・ name.rda: ⼆二進位R資料檔。︒。(⽤用load(name)載⼊入)

Page 6: Statistical computing 01

數學運算> 3 - 4 [1] -1

> 5 * 6 [1] 30

> 7 / 8 [1] 0.875

> 1 + 2 * 3 [1] 7

> (1 + 2) * 3 [1] 9

> 15 / 4 [1] 3.75

> 15 %% 4 [1] 3

> 2^2 [1] 4

> 2^0.5 [1] 1.414214

> 2^ 4.3 [1] 19.69831

> 2^-0.5 [1] 0.7071068

log(4) # natural log

log10(4) # log in base 10

log(4,10) # same as above

sqrt(9) # square root

abs(3-4) # absolute value

exp(1) # exponential

Page 7: Statistical computing 01

R Packages•・ Packages 之於 R users, 如同神奇寶⾙貝之於訓練家們

Page 8: Statistical computing 01

R Packages•・ 神奇寶⾙貝的使⽤用守則:

•・ 捕捉、︑、招喚、︑、進化、︑、離開訓練師

Page 9: Statistical computing 01

R Packages•・ Select repositories... => 選擇套件所在資源庫 => OK

•・ Packages => Install package(s) => CRAN mirror => installr, rgl, scatterplot3d (按Ctrl可多重選取) => OK

> install.packages(c(“rgl","scatterplot3d","Rcmdr"))

> install.packages("foreign", repos = "http://cran.csie.ntu.edu.tw")

> library(rgl) # 載⼊入套件,same as > library("rgl")

> detach(package:rgl) # 卸載套件

> remove.packages("rgl") # 移除套件

> .libPaths() # 列出library安裝⽬目錄

Page 10: Statistical computing 01

R Packages/helps•・ library(help=“[package name]”) # 檢視套件內容

> library(help=“stats")

•・ ?[package name] # 簡略說明

> ?stats

•・ ?[package name]::[function name] # 函數使⽤用說明

> ?stats::lm

Page 11: Statistical computing 01

求助範例 (conti.) 說明

語法

參數

回傳值

範例

Page 12: Statistical computing 01

•・ R packages 提供的實作範例

>demo()

>demo(package = .packages(all.available = TRUE))

>demo(stats::lm.glm)

Page 13: Statistical computing 01

建⽴立 R documents 的好習慣•・ 利⽤用注解 (##) 在documents 中說明⽤用途與修改歷程

•・ e.g., ⽇日期、︑、主要功能、︑、套件和主程式的版本等等

### This is for …. By xxx at 2014/7/06

library(ez)

setwd(“c:/data/”)

rm(list=ls())

load(“myexample.Rdata”)

Page 14: Statistical computing 01

http://www.hmwu.idv.tw

更新R軟體及R套件■ 更新R軟體

■ 反安裝舊版本 R,安裝新版本R。複製所有套件⺫⽬目錄⾄至新本R。執⾏行套件更 新後,刪除舊檔案。update.packages(checkBuilt=TRUE,

■ 利⽤用installr套件 Rstudio => Tools => Install Packages

ask=FALSE)

■ 更新R套件 ■ RGui => Packages => Update packages ■ RStudio => Tools => Check for Package Updates...

> update.packages() > update.packages(ask=FALSE)

Page 15: Statistical computing 01

http://www.hmwu.idv.tw

> version_ x86_64-w64-mingw32 x86_64 mingw32 x86_64, mingw32

platform arch os system status major minor year month day svn rev language version.string nickname

3 0.2 2013 09 25 63987 R R version 3.0.2 (2013-09-25) Frisbee Sailing

利⽤用installr套件更新R軟體> library(installr) > installr()

Page 16: Statistical computing 01

http://www.hmwu.idv.tw

> iris

• 開放資料Open Data http://www.opendata.tw/

Most Frequently Used Data Sets The sepal length, sepal width, petal length, andUCI Machine Learning Repository

https://archive.ics.uci.edu/ml/

• StatLib: http://lib.stat.cmu.edu/ • 政府資料開放平台

http://data.gov.tw/

2014 IASC Data Analysis Competition: http://www.iasc-isi.org/node/227

- World Bank - http://data.worldbank.org - United Nations - http://www.un.org/en/databases/#stats - World Health Organization - http://www.who.int/research/en/

petal width are measured in centimeters on 50 iris specimens from each of three species, Iris setosa, I. versicolor, and I. virginica. Fisher (1936)