Adatelemzés II. [SST35] Idősorok 0. Lőw András low.andras@gmail.com 2011. október 26. Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 1 / 24
Vázlat 1 Honnan tudja a summary, hogy éppen mi a dolga? 2 lubridate Dátumok és idők könnyedén 3 Objektumorientáltság az R-ben 4 Egy apró példa S4-ben Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 2 / 24
Mellékelt adatok: Orange Így alakult az öt narancsfa átmérője: > summary(orange Tree age circumference 3:7 Min. : 118.0 Min. : 30.0 1:7 1st Qu.: 484.0 1st Qu.: 65.5 5:7 Median :1004.0 Median :115.0 2:7 Mean : 922.1 Mean :115.9 4:7 3rd Qu.:1372.0 3rd Qu.:161.5 Max. :1582.0 Max. :214.0 > str(orange Classes nfngroupeddata, nfgroupeddata, groupeddata and 'data.frame': 35 obs. of 3 variables: $ Tree : Ord.factor w/ 5 levels "3"<"1"<"5"<"2"<..: 2 2 2 2 2 2 2 4 4 4... $ age : num 118 484 664 1004 1231... $ circumference: num 30 58 87 115 120 142 145 33 69 111... - attr(*, "formula"=class 'formula' length 3 circumference ~ age Tree....- attr(*, ".Environment"=<environment: R_EmptyEnv> - attr(*, "labels"=list of 2..$ x: chr "Time since December 31, 1968"..$ y: chr "Trunk circumference" - attr(*, "units"=list of 2..$ x: chr "(days"..$ y: chr "(mm" Forrás: Draper, N. R. and Smith, H. (1998, Applied Regression Analysis (3rd ed, Wiley (exercise 24.N. Pinheiro, J. C. and Bates, D. M. (2000 Mixed-e ects Models in S and S-PLUS, Springer. Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 3 / 24
Így gyarapodtak a narancsfák: 3 Orange dataset by Trees 1 5 200 150 100 50 circumference [mm] 200 2 4 150 100 50 500 1000 1500 500 1000 1500 age [days] 500 1000 1500 Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 4 / 24
Az 5 fa 7 alkalommal megmért kerülete: Orange dataset 4 5 count 3 2 1 count 4 3 2 1 age 1582 1372 1231 1004 664 484 118 0 0 50 100 150 200 circumference 500 1000 1500 age 4 5 3 4 Tree 3 4 count 2 count 2 2 5 1 3 1 1 0 0 50 100 150 200 circumference 500 1000 1500 age Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 5 / 24
Mellékelt adatok: Loblolly Ez pedig néhány fenyőfa magasságáról szól: > summary(loblolly height age Seed Min. : 3.46 Min. : 3.0 329 : 6 1st Qu.:10.47 1st Qu.: 5.0 327 : 6 Median :34.00 Median :12.5 325 : 6 Mean :32.36 Mean :13.0 307 : 6 3rd Qu.:51.36 3rd Qu.:20.0 331 : 6 Max. :64.10 Max. :25.0 311 : 6 (Other:48 > str(loblolly Classes nfngroupeddata, nfgroupeddata, groupeddata and 'data.frame': 84 obs. of 3 variables: $ height: num 4.51 10.89 28.72 41.74 52.7... $ age : num 3 5 10 15 20 25 3 5 10 15... $ Seed : Ord.factor w/ 14 levels "329"<"327"<"325"<..: 10 10 10 10 10 10 13 13 13 13... - attr(*, "formula"=class 'formula' length 3 height ~ age Seed....- attr(*, ".Environment"=<environment: R_EmptyEnv> - attr(*, "labels"=list of 2..$ x: chr "Age of tree"..$ y: chr "Height of tree" - attr(*, "units"=list of 2..$ x: chr "(yr"..$ y: chr "(ft" Forrás: Kung, F. H. (1986, Fitting logistic growth curve with predetermined carrying capacity, in Proceedings of the Statistical Computing Section, American Statistical Association, 340 343. Pinheiro, J. C. and Bates, D. M. (2000 Mixed-e ects Models in S and S-PLUS, Springer. Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 6 / 24
Így nőttek a fenyőfák: Loblolly dataset 60 50 Seed 40 305 303 309 323 301 height [ft] 30 319 321 315 311 331 307 325 20 327 329 10 5 10 15 20 25 age [years] Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 7 / 24
További summary: Linear Model(1 > summary(swiss Fertility Agriculture Examination Education Catholic Min. :35.00 Min. : 1.20 Min. : 3.00 Min. : 1.00 Min. : 2.150 1st Qu.:64.70 1st Qu.:35.90 1st Qu.:12.00 1st Qu.: 6.00 1st Qu.: 5.195 Median :70.40 Median :54.10 Median :16.00 Median : 8.00 Median : 15.140 Mean :70.14 Mean :50.66 Mean :16.49 Mean :10.98 Mean : 41.144 3rd Qu.:78.45 3rd Qu.:67.65 3rd Qu.:22.00 3rd Qu.:12.00 3rd Qu.: 93.125 Max. :92.50 Max. :89.70 Max. :37.00 Max. :53.00 Max. :100.000 Infant.Mortality Min. :10.80 1st Qu.:18.15 Median :20.00 Mean :19.94 3rd Qu.:21.70 Max. :26.60 > summary(lm(fertility ~., data = swiss Call: lm(formula = Fertility ~., data = swiss Residuals: Min 1Q Median 3Q Max -15.2743-5.2617 0.5032 4.1198 15.3213 Coefficients: Estimate Std. Error t value Pr(> t (Intercept 66.91518 10.70604 6.250 1.91e-07 *** Agriculture -0.17211 0.07030-2.448 0.01873 * Examination -0.25801 0.25388-1.016 0.31546 Education -0.87094 0.18303-4.758 2.43e-05 *** Catholic 0.10412 0.03526 2.953 0.00519 ** Infant.Mortality 1.07705 0.38172 2.822 0.00734 ** --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05. 0.1 1 Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 8 / 24
További summary: Generalized Linear Model(2 Treat Prewt Postwt CBT :29 Min. :70.00 Min. : 71.30 Cont:26 1st Qu.:79.60 1st Qu.: 79.33 FT :17 Median :82.30 Median : 84.05 Mean :82.41 Mean : 85.17 3rd Qu.:86.00 3rd Qu.: 91.55 Max. :94.90 Max. :103.60 > summary(glm(postwt ~ Prewt + Treat + offset(prewt, family = gaussian, data = anorexia Call: glm(formula = Postwt ~ Prewt + Treat + offset(prewt, family = gaussian, data = anorexia Deviance Residuals: Min 1Q Median 3Q Max -14.1083-4.2773-0.5484 5.4838 15.2922 Coefficients: Estimate Std. Error t value Pr(> t (Intercept 49.7711 13.3910 3.717 0.000410 *** Prewt -0.5655 0.1612-3.509 0.000803 *** TreatCont -4.0971 1.8935-2.164 0.033999 * TreatFT 4.5631 2.1333 2.139 0.036035 * --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05. 0.1 1 (Dispersion parameter for gaussian family taken to be 48.69504 Null deviance: 4525.4 on 71 degrees of freedom Residual deviance: 3311.3 on 68 degrees of freedom AIC: 489.97 Number of Fisher Scoring iterations: 2 Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 9 / 24
További summary: table (3 > apply(ucbadmissions, c(1, 2, sum Gender Admit Male Female Admitted 1198 557 Rejected 1493 1278 > UCBAdmissions,, Dept = A Gender Admit Male Female Admitted 512 89 Rejected 313 19,, Dept = B Gender Admit Male Female Admitted 353 17 Rejected 207 8,, Dept = C Gender Admit Male Female Admitted 120 202 Rejected 205 391 > summary(ucbadmissions Number of cases in table: 4526 Number of factors: 3 Test for independence of all factors: Chisq = 2000.3, df = 16, p-value = 0 > apply(ucbadmissions, 3, function(u summary(as.table(u $A Number of cases in table: 933 Number of factors: 2 Test for independence of all factors: Chisq = 17.248, df = 1, p-value = 3.28e-05 $B Number of cases in table: 585 Number of factors: 2 Test for independence of all factors: Chisq = 0.25372, df = 1, p-value = 0.6145 $C Number of cases in table: 918 Number of factors: 2 Test for independence of all factors: Chisq = 0.7535, df = 1, p-value = 0.3854 $D Number of cases in table: 792 Number of factors: 2,, Dept = D Test for independence of all factors: Chisq = 0.29798, df = 1, p-value = 0.5852 Gender Admit Male Female $E Admitted 138 131 Number of cases in table: 584 Rejected 279 244 Lőw A (low.andras@gmail.com Number Adatelemzés of factors: II. [SST35] 2 2011. október 26. 10 / 24
Vázlat 1 Honnan tudja a summary, hogy éppen mi a dolga? 2 lubridate Dátumok és idők könnyedén 3 Objektumorientáltság az R-ben 4 Egy apró példa S4-ben Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 11 / 24
Mire jó a lubridate csomag? Az alap R: > ( r.date <- as.posixct('01-01-2011', format = '%d-%m-%y', tz = 'UTC' [1] "2011-01-01 UTC" > as.numeric(format(r.date, '%m' [1] 1 > r.date <- as.posixct(format(r.date, '%Y-2-%d', tz = 'UTC' > r.date [1] "2011-02-01 UTC" > seq(r.date, length = 2, by = '-1 day'[2] [1] "2011-01-31 UTC" > as.posixct(format(as.posixct(r.date, tz = 'UTC', tz = 'GMT' [1] "2011-02-01 GMT" A lubridate csomag: > ( l.date <- dmy('01-01-2011' [1] "2011-01-01 UTC" > month(l.date [1] 1 > month(l.date <- 2 > l.date [1] "2011-02-01 UTC" > l.date - days(1 [1] "2011-01-31 UTC" > with_tz(l.date, 'GMT' [1] "2011-02-01 GMT" Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 12 / 24
Vázlat 1 Honnan tudja a summary, hogy éppen mi a dolga? 2 lubridate Dátumok és idők könnyedén 3 Objektumorientáltság az R-ben 4 Egy apró példa S4-ben Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 13 / 24
Rövid történelem Az első megoldás 1990 körül jelent meg az S 3-as verziójában, ezért S3. https://github.com/hadley/devtools/wiki/s3 A következő verzióban vezették be a többszörös argumentumokat, javítottak az öröklődésen és absztraktabb lett: S4. https://github.com/hadley/devtools/wiki/s4 Tavaly lépett tovább ezen is az R: R5. https://github.com/hadley/devtools/wiki/r5 Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 14 / 24
Vázlat 1 Honnan tudja a summary, hogy éppen mi a dolga? 2 lubridate Dátumok és idők könnyedén 3 Objektumorientáltság az R-ben 4 Egy apró példa S4-ben Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 15 / 24
Fogyókúra-napló (1 Először készítsünk házilag idősort! Kell hozzá: kezdő időpont, záró időpont, adat (természetesen. > setclass('timeseries', representation( data = 'numeric', start = 'POSIXct', end = 'POSIXct' [1] "TimeSeries" > my.timeseries <- new('timeseries', data = c(1, 2, 3, 4, 5, 6, start = as.posixct('10/01/2011 0:00:00', tz = 'CEST', format = '%m/%d/%y %H:%M:%S', end = as.posixct('10/01/2011 0:05:00', tz = 'CEST', format = '%m/%d/%y %H:%M:%S' > my.timeseries An object of class "TimeSeries" Slot "data": [1] 1 2 3 4 5 6 Slot "start": [1] "2011-10-01 UTC" Slot Lőw"end": A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 16 / 24
Fogyókúra-napló (2 Mikor érvényes egy idősor? van kezdő időpontja, van záró időpontja, akezdetazárásnálkorábbi(természetesen. > setvalidity('timeseries', function(object { object@start <= object@end && length(object@start == 1 && length(object@end == 1 } Class "TimeSeries" [in ".GlobalEnv"] Slots: Name: data start end Class: numeric POSIXct POSIXct > validobject(my.timeseries [1] TRUE Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 17 / 24
Fogyókúra-napló (3 Most már minden újabb elem felvételekor automatikusan lefut az érvényesség ellenőrzése is. Nem lehetünk ezért elég hálásak! > good.timeseries <- new('timeseries', data = c(7, 8, 9, 10, 11, 12, start = as.posixct('10/01/2011 0:06:00', tz = 'CEST', format = '%m/%d/%y %H:%M:%S', end = as.posixct('10/01/2011 0:11:00', tz = 'CEST', format = '%m/%d/%y %H:%M:%S' > bad.timeseries <- new('timeseries', data = c(7, 8, 9, 10, 11, 12, start = as.posixct('10/01/2011 0:06:00', tz = 'CEST', format = '%m/%d/%y %H:%M:%S', end = as.posixct('10/01/2008 0:11:00', tz = 'CEST', format = '%m/%d/%y %H:%M:%S' Error in validobject(.object : invalid class "TimeSeries" object: FALSE Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 18 / 24
Fogyókúra-napló (4 Mekkora időszakot fog át az idősorunk? Mik az adatok? Az első függvényeink [method] az új osztályhoz. > period.timeseries <- function(object { if (length(object@data > 1 { (object@end - object@start / (length(object@data - 1 } else { Inf } } > series <- function(object {object@data} > setgeneric('series' [1] "series" > series(my.timeseries [1] 1 2 3 4 5 6 > showmethods('series' Function: series (package.globalenv object="any" object="timeseries" (inherited from: object="any" Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 19 / 24
Fogyókúra-napló (5 Kössük a period.timeseries-t, a TimeSeries osztályhoz! > period <- function(object {object@period} > setgeneric('period' [1] "period" > setmethod(period, signature = 'TimeSeries', definition = period.timeseries [1] "period" attr(,"package" [1] ".GlobalEnv" > showmethods('period' Function: period (package.globalenv object="any" object="timeseries" > period(my.timeseries [1] 2011-10-01 -- 2011-10-01 00:01:00 Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 20 / 24
Fogyókúra-napló (6 A summary és a darabolás [ nem maradhat ki! > setmethod('summary', signature = 'TimeSeries', definition = function(object { print(paste(object@start, ' to ', object@end, sep = '', collapse = '' print(paste(object@data, sep = '', collapse = ', ' } [1] "summary" > summary(my.timeseries [1] "2011-10-01 to 2011-10-01 00:05:00" [1] "1, 2, 3, 4, 5, 6" > # > setmethod('[', signature = 'TimeSeries', definition = function(x, i, j,..., drop { x@data[i] } [1] "[" > my.timeseries[3] [1] 3 Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 21 / 24
Fogyókúra-napló (7 Eddig még szó sem volt a naplóról! > setclass('weighthistory', representation( height = 'numeric', name = 'character', contains = 'TimeSeries' [1] "WeightHistory" > john.doe <- new('weighthistory', data = c(170, 169, 171, 168, 170, 169, start = as.posixct('08/14/2011 0:00:00', tz = 'CEST', format = '%m/%d/%y %H:%M:%S', end = as.posixct('09/28/2011 0:00:00', tz = 'CEST', format = '%m/%d/%y %H:%M:%S', height = 72, name = 'John Doe' > john.doe An object of class "WeightHistory" Slot "height": [1] 72 Slot "name": [1] "John Doe" Slot "data": [1] 170 169 171 168 170 169 Slot "start": [1] "2011-08-14 UTC" Slot "end": [1] "2011-09-28 UTC" Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 22 / 24
Fogyókúra-napló (8 További öröklések: > setclass( 'Person', representation( height = 'numeric', name = 'character' [1] "Person" > # > setclass( 'AltWeightHistory', contains = c('timeseries', 'Person' [1] "AltWeightHistory" > # > setclass( 'Cat', representation( breed = 'character', name = 'character' [1] "Cat" > # > setclassunion( 'NamedThing', c('person','cat' [1] "NamedThing" Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 23 / 24
Fogyókúra-napló (9 Minden együtt: > jane.doe <- new('altweighthistory', data = c(130, 129, 131, 128, 130, 129, start = as.posixct('08/14/2011 0:00:00', tz = 'CEST', format = '%m/%d/%y %H:%M:%S', end = as.posixct('09/28/2011 0:00:00', tz = 'CEST', format = '%m/%d/%y %H:%M:%S', height = 67, name = 'Jane Doe' > jane.doe An object of class "AltWeightHistory" Slot "data": [1] 130 129 131 128 130 129 Slot "start": [1] "2011-08-14 UTC" Slot "end": [1] "2011-09-28 UTC" Slot "height": [1] 67 Slot "name": [1] "Jane Doe" > is(jane.doe,'namedthing' [1] TRUE > is(john.doe,'timeseries' [1] TRUE Lőw A (low.andras@gmail.com Adatelemzés II. [SST35] 2011. október 26. 24 / 24