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

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

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

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)

Support Manual HoistLocatel Electronic Locks

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

Beijer Electronics AB 2000, MA00336A,

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

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

Module 1: Functions, Limits, Continuity

Preschool Kindergarten


Installation av F13 Bråvalla

Module 6: Integrals and applications

Tentamen i Matematik 2: M0030M.

Webbregistrering pa kurs och termin

Isometries of the plane

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

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

12.6 Heat equation, Wave equation

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

Webbreg öppen: 26/ /

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.

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

Workplan Food. Spring term 2016 Year 7. Name:

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

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

Pre-Test 1: M0030M - Linear Algebra.

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

Boiler with heatpump / Värmepumpsberedare

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

Solutions to exam in SF1811 Optimization, June 3, 2014

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

Isolda Purchase - EDI

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

M0030M: Maple Laboration

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

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

Stockholms Univ., Statistiska Inst. Finansiell Statistik, GN, 7,5 hp, HT2008 Några frågor och svar rörande inlämningsuppgiften

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

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

Chapter 2: Random Variables

FORTA M315. Installation. 218 mm.

Provlektion Just Stuff B Textbook Just Stuff B Workbook

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

Writing with context. Att skriva med sammanhang

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

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


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

Consumer attitudes regarding durability and labelling

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

Quick Start Guide Snabbguide

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

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

PRESS FÄLLKONSTRUKTION FOLDING INSTRUCTIONS

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

Tentamen MMG610 Diskret Matematik, GU

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

Eternal Employment Financial Feasibility Study

Problem som kan uppkomma vid registrering av ansökan

6 th Grade English October 6-10, 2014

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

PRESS FÄLLKONSTRUKTION FOLDING INSTRUCTIONS

Calculate check digits according to the modulus-11 method

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

samhälle Susanna Öhman

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

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

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

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

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

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

Questionnaire for visa applicants Appendix A

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

Tentamen i Matematik 2: M0030M.

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

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

Windlass Control Panel v1.0.1

S 1 11, S 2 9 and S 1 + 2S 2 32 E S 1 11, S 2 9 and 33 S 1 + 2S 2 41 D S 1 11, S 2 9 and 42 S 1 + 2S 2 51 C 52 S 1 + 2S 2 60 B 61 S 1 + 2S 2 A

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

Styrteknik : Funktioner och funktionsblock

Hjälpmedel: Inga, inte ens miniräknare Göteborgs Universitet Datum: 2018 kl Telefonvakt: Jonatan Kallus Telefon: ankn 5325

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

Vad kännetecknar en god klass. Vad kännetecknar en god klass. F12 Nested & Inner Classes

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

2(x + 1) x f(x) = 3. Find the area of the surface generated by rotating the curve. y = x 3, 0 x 1,

Recitation 4. 2-D arrays. Exceptions

EXTERNAL ASSESSMENT SAMPLE TASKS SWEDISH BREAKTHROUGH LSPSWEB/0Y09

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

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

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

2.45GHz CF Card Reader User Manual. Version /09/15

The Algerian Law of Association. Hotel Rivoli Casablanca October 22-23, 2009

[HUR DU ANVÄNDER PAPP] Papp är det program som vi nyttjar för att lotta turneringar och se resultat.

Accomodations at Anfasteröd Gårdsvik, Ljungskile

Service och bemötande. Torbjörn Johansson, GAF Pär Magnusson, Öjestrand GC

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.

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

Documentation SN 3102

Transkript:

Stockholms Univ., Statistiska Inst. Finansiell Statistik Instruktioner till R Nicklas Pettersson 1 Extra 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. Dessutom är programmet mycket mer exibelt än många andra statistikprogram. 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 Tinn-R. (Denna nns dock inte installerad i datorsalen, men kanske kommer.) Kod ler skapas i R genom att välja File/New script. Hela eller delar av koden körs genom att markera koden och sedan trycka ctrl + r, eller så väljs kommandet Edit/Run via menyn. 1.1 Instruktioner och manualer En uppsjö av böcker och annan litteratur nns skrivet om programmet, men för att lösa inlämningsuppgiften kommer det att vara tillräckligt att man följer med på datorövningarna. Sidan http://gauss.stat.su.se/f/r/ är en studiecirkel som statistiska institutionen hade under 2008. 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. 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) # To add a line for FondB, write lines(datum,variable[,3,36],lty=2) # If the line doesn t show up, this is probably because it is out of range of the ylimits. Then change y-limits plot(datum,variable[,2,36],xlab="2008",ylab="fonda",type="l",lty=1,ylim=c(0,3000)) lines(datum,variable[,3,36],lty=2) 1.8 Some statistical models 1.8.1 Linear model MYlmMODELL <- 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 MYlmMODELL 1.8.2 Holt Winter regression HWmodel <- HoltWinters(vector,alfa,beta,gamma) # Holt Winters exponential smoothing with parameters saved in HWmodel HWmodel <- HoltWinters(vector,0.2,0.4,0) # Holt Winters with alfa=0.2, beta=0.4 and non-seasonal model saved in HWmodel predict(hwmodel,nrahead,prediction.interval=t) # Predicts HWmodel, nrahead steps ahead, and if prediction.interval is T=TRUE prediction intervals are given. Thus to predict HWmodel one step ahead with 95% prediction interval you could write: predict(hwmodel,1,t) 5

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 1.9 Save and load your data and code The best thing to do is to save your code in a le. If From within R 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