Stockholms Univ., Statistiska Inst. Finansiell Statistik, GN, 7,5 hp, HT2008 Instruktioner till R

Relevanta dokument
Stockholms Univ., Statistiska Inst. Finansiell Statistik Instruktioner till R

Stockholms Univ., Statistiska Inst. Finansiell Statistik, GN, 7,5 hp, HT2008 Numeriska svar till övningar

1. Compute the following matrix: (2 p) 2. Compute the determinant of the following matrix: (2 p)

This exam consists of four problems. The maximum sum of points is 20. The marks 3, 4 and 5 require a minimum

1. Varje bevissteg ska motiveras formellt (informella bevis ger 0 poang)

Styrteknik: Binära tal, talsystem och koder D3:1

Beijer Electronics AB 2000, MA00336A,

Isometries of the plane

2.1 Installation of driver using Internet Installation of driver from disk... 3

Support Manual HoistLocatel Electronic Locks

Tentamen i Matematik 2: M0030M.

Module 6: Integrals and applications

Webbregistrering pa kurs och termin

Module 1: Functions, Limits, Continuity

Preschool Kindergarten


12.6 Heat equation, Wave equation

Installation av F13 Bråvalla

Webbreg öppen: 26/ /

Kurskod: TAMS28 MATEMATISK STATISTIK Provkod: TEN1 05 June 2017, 14:00-18:00. English Version

Laboration med MINITAB, Del 2 Om Fyris ns global uppv rmning

denna del en poäng. 1. (Dugga 1.1) och v = (a) Beräkna u (2u 2u v) om u = . (1p) och som är parallell

8 < x 1 + x 2 x 3 = 1, x 1 +2x 2 + x 4 = 0, x 1 +2x 3 + x 4 = 2. x 1 2x 12 1A är inverterbar, och bestäm i så fall dess invers.

Pre-Test 1: M0030M - Linear Algebra.

Schenker Privpak AB Telefon VAT Nr. SE Schenker ABs ansvarsbestämmelser, identiska med Box 905 Faxnr Säte: Borås

Make a speech. How to make the perfect speech. söndag 6 oktober 13

Isolda Purchase - EDI

and u = och x + y z 2w = 3 (a) Finn alla lösningar till ekvationssystemet

Solutions to exam in SF1811 Optimization, June 3, 2014

FÖRBERED UNDERLAG FÖR BEDÖMNING SÅ HÄR

Annonsformat desktop. Startsida / områdesstartsidor. Artikel/nyhets-sidor. 1. Toppbanner, format 1050x180 pxl. Format 1060x180 px + 250x240 pxl.

Boiler with heatpump / Värmepumpsberedare

M0030M: Maple Laboration

Consumer attitudes regarding durability and labelling

F ξ (x) = f(y, x)dydx = 1. We say that a random variable ξ has a distribution F (x), if. F (x) =

Workplan Food. Spring term 2016 Year 7. Name:

Kurskod: TAIU06 MATEMATISK STATISTIK Provkod: TENA 15 August 2016, 8:00-12:00. English Version

Lösenordsportalen Hosted by UNIT4 For instructions in English, see further down in this document


Quick Start Guide Snabbguide

Översättning av galleriet. Hjälp till den som vill...

Technique and expression 3: weave. 3.5 hp. Ladokcode: AX1 TE1 The exam is given to: Exchange Textile Design and Textile design 2.

(D1.1) 1. (3p) Bestäm ekvationer i ett xyz-koordinatsystem för planet som innehåller punkterna

Exempel på uppgifter från 2010, 2011 och 2012 års ämnesprov i matematik för årskurs 3. Engelsk version

Provlektion Just Stuff B Textbook Just Stuff B Workbook

How to format the different elements of a page in the CMS :

Chapter 2: Random Variables

FORTA M315. Installation. 218 mm.

Problem som kan uppkomma vid registrering av ansökan

PRESS FÄLLKONSTRUKTION FOLDING INSTRUCTIONS

Information technology Open Document Format for Office Applications (OpenDocument) v1.0 (ISO/IEC 26300:2006, IDT) SWEDISH STANDARDS INSTITUTE

Calculate check digits according to the modulus-11 method

Kurskod: TAIU06 MATEMATISK STATISTIK Provkod: TENA 17 August 2015, 8:00-12:00. English Version

Kvalitetsarbete I Landstinget i Kalmar län. 24 oktober 2007 Eva Arvidsson

Eternal Employment Financial Feasibility Study

This is England. 1. Describe your first impression of Shaun! What kind of person is he? Why is he lonely and bullied?

Statistical Quality Control Statistisk kvalitetsstyrning. 7,5 högskolepoäng. Ladok code: 41T05A, Name: Personal number:

Grafisk teknik IMCDP IMCDP IMCDP. IMCDP(filter) Sasan Gooran (HT 2006) Assumptions:

6 th Grade English October 6-10, 2014

PRESS FÄLLKONSTRUKTION FOLDING INSTRUCTIONS

LUNDS TEKNISKA HÖGSKOLA Institutionen för Elektro- och Informationsteknik

Tentamen MMG610 Diskret Matematik, GU

Tentamen i Matematik 2: M0030M.

Materialplanering och styrning på grundnivå. 7,5 högskolepoäng

Det finns en handledning till kortet på hemsidan. AVR STK500.

2. Lära sig beskriva en variabel numeriskt med "proc univariate" 4. Lära sig rita diagram med avseende på en annan variabel

Writing with context. Att skriva med sammanhang

Read Texterna består av enkla dialoger mellan två personer A och B. Pedagogen bör presentera texten så att uttalet finns med under bearbetningen.

Kurskod: TAMS11 Provkod: TENB 28 August 2014, 08:00-12:00. English Version

Recitation 4. 2-D arrays. Exceptions

Grafisk teknik IMCDP. Sasan Gooran (HT 2006) Assumptions:

Authentication Context QC Statement. Stefan Santesson, 3xA Security AB

1. Unpack content of zip-file to temporary folder and double click Setup

Every visitor coming to the this website can subscribe for the newsletter by entering respective address and desired city.

Accomodations at Anfasteröd Gårdsvik, Ljungskile

Grafisk teknik. Sasan Gooran (HT 2006)

Schenker Privpak AB Telefon VAT Nr. SE Schenker ABs ansvarsbestämmelser, identiska med Box 905 Faxnr Säte: Borås

DVG C01 TENTAMEN I PROGRAMSPRÅK PROGRAMMING LANGUAGES EXAMINATION :15-13: 15

Statistikens grunder 1 och 2, GN, 15 hp, deltid, kvällskurs

Libers språklåda i engelska Grab n go lessons

Read, work and talk! - och Lgr 11

1. Find an equation for the line λ which is orthogonal to the plane

Samverkan på departementsnivå om Agenda 2030 och minskade hälsoklyftor

Questionnaire for visa applicants Appendix A

EXTERNAL ASSESSMENT SAMPLE TASKS SWEDISH BREAKTHROUGH LSPSWEB/0Y09

Windlass Control Panel v1.0.1

Immigration Studying. Studying - University. Stating that you want to enroll. Stating that you want to apply for a course.

samhälle Susanna Öhman

Viktig information för transmittrar med option /A1 Gold-Plated Diaphragm

x 2 2(x + 2), f(x) = by utilizing the guidance given by asymptotes and stationary points. γ : 8xy x 2 y 3 = 12 x + 3

f(x) =, x 1 by utilizing the guidance given by asymptotes and stationary points. cos(x) sin 3 (x) e sin2 (x) dx,

Support for Artist Residencies

Övning 3 - Tillämpad datalogi 2012

Resa Att ta sig runt. Att ta sig runt - Platser. Du vet inte var du är. Be om att bli visad en viss plats på en karta. Fråga om en viss servicepunkt

Resa Att ta sig runt. Att ta sig runt - Platser. I am lost. Du vet inte var du är

Discovering!!!!! Swedish ÅÄÖ. EPISODE 6 Norrlänningar and numbers Misi.se

LÄNKHJUL S3. Monteringsanvisning för: Länkhjul S3

NO NEWS ON MATRIX MULTIPLICATION. Manuel Kauers Institute for Algebra JKU

Småprat Small talk (stressed vowels are underlined)

Styrteknik : Funktioner och funktionsblock

Transkript:

Stockholms Univ., Statistiska Inst. Finansiell Statistik, GN, 7,5 hp, HT2008 Instruktioner till R Nicklas Pettersson 1 Instruktioner till R En av fördelarna med R är att det är gratis, varför vem som helst kan ladda hem det och använda det på vilken dator som helst utan att frågor om upphovsrätt och annat uppkommer. Vidare nns en massa andra fördelar såsom dess exibilitet, men det hoppas jag att ni kommer att upptäcka själva! :) Programmets egen hemsida med nedladdning, diverse manualer, newsgroup för att ställa frågor (kräver registrering), tilläggspacket mm är: http://www.r-project.org/ och en länk till den senaste windowsversionen (2008-12-10) är: http://ftp.sunet.se/pub/lang/cran/bin/windows/base/r-2.8.0-win32.exe Om man vill använda en annan editor än den som nns i R så rekommenderas bl a Tinn-R. (Finns dock inte installerad i datorsalen, men kanske kommer.) 1.1 Instruktioner och manualer En uppsjö av böcker och annan litteratur nns skrivet om programmet, men för det som gås igenom på övningar kommer det förhoppningsvis vara tillräckligt att man följer med på datorövningarna och tar del av innehållet i något/några av följande kompendier och sammanställningar: Sidan http://gauss.stat.su.se/f/r/ är en studiecirkel som statistiska institutionen hade denna termin. Framförallt kan delar av slides 1 och 2 vara intressanta för er (kanske också de slides som berör gra k). Den text som rekommenderas för alla nybörjare är "An introduction to R" som nås genom att klicka på "Manuals" via programmets hemsida (eller via denna länk http://cran.r-project.org/doc/manuals/r-intro.pdf ). Här nns också diverse andra manualer. 1

En sida jag tycker är bra (i synnerhet för den som tidigare använd t ex SAS eller SPSS) är http://www.statmethods.net/index.html. Här åter nns bl a manualen http://oit.utk.edu/scc/rforsas&spssusers.pdf. På nationalekonomiska nns följande manual med fokus på ekonometri: http://www.ne.su.se/education/grundniva/nekii_iii/ht08/empirical1/r_intro.pdf På Uppsala Universitet nns följande sida om "STATISTICAL PRO- GRAMMING with R" http://www.dis.uu.se/kurser/kurs.php?cid=380. Dessutom är programmets egna inbyggda hjälpfunktioner ofta bra (kan dock variera både i innehåll och svårighetsgrad). Skriv helt enkelt "?kommando" om ni vill ha hjälp med ett speci kt kommando. Vidare går mycket att hitta genom att söka på nätet. Ofta hamnar man då i newsgroupen. 1.2 General commands # anything everything to the right of the sign # is ignored by R (in this case the word anything). So # can be used for making comments?command or help(command) # get help with the command example(command) # get an example of command rm(anything) # remove the object anything rm(list=ls()) # remove (almost) everything in workspace, use with caution!! ls() # list all objects in workspace 1.3 Commands for calculations and how to deal with vectors 1 +2 # one plus two x <- 1+2 # x = 1+2 1+2 -> x y <- 2*x # two multiplied with x z <- 1:4 # z <- c(1,2,3,4) z<-seq(1,4,1) z <- seq(to=4,from=1,by=1) z*x # z multiplied with x z*x-1 # z multiplied with x, all elements minus one z*(x-1) # z multiplied with x minus one sqrt(z) # z^0.5 calculates squared root of z rep(z,each=3) # Repeat each element in z three times rep(z,times=2) # Repeat z two times rep(z,times=2,each=3) # Repeat each element in z three times, and repeat that two times z[1] # rst element in z 2

z[2] # second element in z (which in this example is missing) z[1:2] # z[c(1,2)] both rst and second element in z z[-3] # all elements in z except the third z<3 # which elements in z are <3 z[z>3] # all elements where z>3 z[z>3 j z<2] # all elements where z>3 or z<2 z[z>3 & z<2] # all elements where z>3 and z<2 (must be none) length(z) # gives length of z is.na(z) # does z include missing values?!is.na(z) # which values in z are not missing values? is.na(z[1:5]) # are the ve rst elements in z missing values? is.nan(z) # is z not a number? y <- c(y,1,na,342) # set y to include elements y(the old one), 1, a missing value and 342 z[is.na(y)==true] # z[is.na(y)==t] show only elements in z for which y is a missing value 1.4 Matrices and datasets rbind(z,y) # set z and y to become rows in a matrix cbind(z,y) # set z and y to be columns in a matrix ZY <- cbind(z,y) # put the vectors in a matrix named ZY. Note that capital and small letters are important so that ZY, Zy, zy and zy are four di erent objects in R. dim(zy) # dimensions of ZY (unless object is a simple vector, then NULL) dim(zy) <- c(1,8) # change dimension of ZY to one row, eight columns dim(zy) <- c(4,2) # change dimension of ZY to four rows, two columns ZY*x # multiplication with x ZY*1:4 # multiplication with 1:4 t(zy)*1:4 # transpose ZY and then multiply with 1:4 t(t(zy)*1:4) # same as above but all transposed (back) ZY[is.na(ZY)==T]<-0 # set all missing values in ZY to 0 ZY[3,2] <- NA # put back the missing value on the third row in the second column x(zy) # Get a window to manipulate ZY, this is similar to "all" other statistical programs ZY <- as.data.frame(zy) # turn the matrix ZY into a data frame. Functions in R are often generic, which means that it might treat the objects di erently depending on the class (vector, matrix, data frame, etc) 3

1.5 Basic statistical commands a <- runif(100) # set a to be 100 random uniform numbers sum(a) # sum of a mean(a) # mean of a median(a) # median of a quantile(a) # usual quantiles of a quantile(a,c(0,0.1,0.6,0.9)) # quantiles 0, 0.1, 0.6 and 0.9 max(a) # max of a min(a) # min of a var(a) # variance of a sd(a) # standard deviation of a cor(a) # correlation of a (why doesn t this work?) b <- rnorm(100) # set b to be 100 random normal numbers ab <- cbind(a,b) # set a and b to be columns in a matrix ab colmeans(ab) # column means of ab colsums(ab) # column sums of ab rowmeans(ab) # row means of ab rowsums(ab) # row sums of ab What if we use the basic statistical commands? Should we coerce ab into a data.frame? 1.6 Tables tabledata <- data.frame(rep(1:10,each=10),sample(1:10,100,replace=true)) # rst column is (1 to 10, 10 times each), second column is (sample from 1 to 10, with replacement) names(tabledata) <- c(" rstcol","secondcol") # Give names to the columns table(tabledata) # cross tabulation of the data 1.7 Plots pie(c(1,4,3,2),labels=c("a","b","c","d")) # pieplot barplot(c(1,4,3,2),names.arg=c("a","b","c","d")) # barplot a2 <- rep(5:1,each=20)+a^2 # manipulate a and put it in a2 b2 <- 1:100+b # manipulate b and put it in b2 hist(a2) plot(a2) plot(a2,b2) # histogram of a2 # plot(a2) # scatterplot of a2 and b2 plot(a2,type="l",lty=1) # plot a2 as a line with linetype 1 in a graph 4

lines(b2,lty=2) # add b2 (default is line) with linetype 2 to the graph plot( a2, type="l",lty=1, ylim = c(min(b2),max(b2))); lines(b2,lty=2) # plot a2 and b2 to in same graph with adjusted y-limits legend(10,100,c("a2line","b2line"),lty=c(1,2)) # add a legend at x position 10 and y position 200, names and linetype as speci ed boxplot(a2) # Make a boxplot of a2 boxplot(data.frame(a2,b2)) # Make boxplots of both a2 and b2 in same graph # Here is how to take the rownames (i.e. dates) from variable, # put them in datum and use them in a plot. I also put labels # on the x and y axis. This example is for group 36, FondA. datum <- format.date(rownames(variable)) datum <- as.date(datum) plot(datum,variable[,2,36],xlab="2008",ylab="fonda",type="l",lty=1) 1.8 Some statistical models 1.8.1 Linear model lm(dep ~indep) # Linear model, where dep is a vector of length=n and indep can be vector (length=n) or matrix (with n rows) MYlmMODELL$ tted.values # Get tted values from the lm model MYlmMODELL 1.8.2 Holt Winter regression HoltWinters(vector,alfa,beta,gamma) # Holt Winters exponential smoothing with parameters HoltWinters(vector,0.2,0.4,0) # Holt Winters with alfa=0.2, beta=0.4 and non-seasonal model predict(hwmodel,nrahead,predint) # Predicts HWmodel, nrahead steps ahead, and if predint is TRUE prediction intervals are given. 1.8.3 ARIMA arima(vector,c(p,d,q)) # arima model where (p, d, q) are the AR order, the degree of di erencing, and the MA order. arima(vector,c(1,0,0)) # This is an autoregressive model of rst order. acf(vector) # autocorrelation function for vector pacf(vector) # partial autocorrelation function for vector 5

1.9 Save and load your data The best thing to do is to save your code in a le. But if you want to load or save a whole workspace, you can do it in the following way. Click on File/Save workspace and save your workspace as lename.rdata. The workspace includes all de ned variables. Or click on File/Load workspace/ lename.rdata to load a workspace into R. 1.10 Packages There are a lot of add-on packages that can be downloaded from http://www.rproject.org/. Click on Packages/Load package and select the package, if it is already installed. Otherwise click on Packages/Install package(s) and select the package. If the package is on the computer you could chose Packages/Install package(s) from local zip les) Some packages require other packages, but these are usually automatically installed. One example is the rgl package, which makes it possible to plot 3D. demo(rgl) # A demo of rgl, plots can be rotated Since no one excpet for the administrator are allowed to install programs, unfortunately you can t use packages in the computer labs. 6