Temat: funkcja w r
Dzieki Adrian za odpowiedz, oto moj problem szczegolowiej:
mam takie zbior danych (ds) podaje pierwsze 30 rzedow:
ds
X Rat Tijd HE MAP Ammo Kreatinine AST Bilitotaal Hb Lact gr pre.treat HPPS survtime
1 1 2 0.0 0.0 115 41 25 110 2 15.3 1.8 1 0 1 11.0
2 2 2 0.5 1.0 108 188 27 220 3 15.0 2.7 1 0 1 11.0
3 3 2 1.0 1.0 89 276 29 1050 3 12.6 3.7 1 0 1 11.0
4 4 2 1.5 2.5 88 625 34 6420 4 13.2 6.0 1 0 1 11.0
5 5 2 2.5 3.0 99 585 43 15580 5 11.5 2.0 1 0 1 11.0
6 6 2 3.5 3.0 107 570 44 18380 6 11.0 2.1 1 0 1 11.0
7 7 2 4.5 3.0 119 625 47 21210 7 11.3 3.0 1 0 1 11.0
8 8 2 5.5 4.0 125 765 51 23640 10 11.6 4.0 1 0 1 11.0
9 9 2 6.5 4.0 135 895 55 23970 15 11.2 4.2 1 0 1 11.0
10 10 2 7.5 4.0 133 985 58 23280 18 10.9 4.9 1 0 1 11.0
11 11 2 8.5 4.5 146 960 66 23240 22 10.5 5.7 1 0 1 11.0
12 12 2 9.5 4.0 146 980 78 21380 24 10.1 7.2 1 0 1 11.0
13 13 2 10.5 4.0 144 1270 97 18220 24 10.8 11.7 1 0 1 11.0
14 14 3 0.0 0.0 108 107 15 30 1 10.2 2.2 1 0 1 8.2
15 15 3 0.5 1.0 94 158 27 160 2 14.4 4.5 1 0 1 8.2
16 16 3 1.0 0.5 94 177 23 440 2 10.8 3.7 1 0 1 8.2
17 17 3 1.5 1.0 92 209 30 2220 4 13.5 4.0 1 0 1 8.2
18 18 3 2.5 1.5 101 448 38 13430 7 13.5 2.9 1 0 1 8.2
19 19 3 3.5 2.0 120 484 39 21290 8 13.3 2.6 1 0 1 8.2
20 20 3 4.5 2.0 130 592 44 30560 10 14.0 3.0 1 0 1 8.2
21 21 3 5.5 4.0 133 850 42 30520 10 13.9 3.7 1 0 1 8.2
22 22 3 6.5 5.0 132 912 58 37580 15 13.7 8.1 1 0 1 8.2
23 23 3 7.5 5.0 160 1144 81 37590 21 13.3 10.1 1 0 1 8.2
24 24 4 0.0 0.0 95 85 15 80 1 11.6 2.9 1 0 1 7.1
25 25 4 0.5 1.0 93 472 23 150 2 15.8 7.2 1 0 1 7.1
26 26 4 1.0 1.0 93 700 29 850 3 16.3 6.4 1 0 1 7.1
27 27 4 1.5 2.0 93 816 35 4340 4 14.9 5.7 1 0 1 7.1
28 28 4 2.5 2.5 103 952 41 9460 5 13.8 3.6 1 0 1 7.1
29 29 4 3.5 2.5 108 808 58 18690 6 13.6 3.6 1 0 1 7.1
30 30 4 4.5 3.0 117 1120 60 23270 7 13.2 2.9 1 0 1 7.1
......
potem robie:
#funkcja liczaca lm model:
get.stat <- function(ratid, varname, ds.time){
ratValues <- subset(ds.time, Rat==ratid)
varVals <- ratValues[,varname];
varTimes <- ratValues$Tijd
if(sum(!is.na(varVals)) < 2) {return(c(ratid, NA, NA, ratValues$survtime[1], ratValues$gr[1], ratValues$pre.treat[1], ratValues$HPPS[1] ))}
lmmodel<-lm(varVals ~ varTimes)
return(c(ratid, lmmodel$coeff[1], lmmodel$coeff[2], ratValues$survtime[1], ratValues$gr[1], ratValues$pre.treat[1], ratValues$HPPS[1]))}
#funkcja dajaca zbior do analizy:
get.stats <- function(varname, ds, timeMax){
ds.time <-subset(ds, Tijd < timeMax, select=c("Rat", "Tijd", varname, "gr", "pre.treat", "HPPS", "survtime"))
return(sapply(unique(ds.time$Rat), get.stat, varname, ds.time))}
i potem robiac recznie, podajac np: timeMax<-4
moge policzyc:
statAmmo <- as.data.frame(t(get.stats("AmmoS", ds, timeMax)))
names(statAmmo) <- c("ID", "s1Ammo", "BetaAmmo", "SurvT", "Grp", "Pre.treat", "Hpps")
statKreat <- as.data.frame(t(get.stats("KreatinineS", ds, timeMax)))
names(statKreat) <- c("ID", "s1Kreat", "BetaKreat", "SurvT", "Grp", "Pre.treat", "Hpps")
statBili <- as.data.frame(t(get.stats("BilitotaalS", ds, timeMax)))
names(statBili) <- c("ID", "s1Bili", "BetaBili", "SurvT", "Grp", "Pre.treat", "Hpps")
i te wyniki zebrac w agg:
agg4<-cbind(statAmmo[,4:7],statAmmo[,1:3],statKreat[,2:3],statBili[,2:3],statHb[,2:3],statHE[,2:3],statMAP[,2:3],statAST[,2:3],statLact[,2:3])
agg4$status <- 1
i potem to samo robie dla timeMax<-7 i wyniki zbieram w:
agg7<-cbind(statAmmo[,4:7],statAmmo[,1:3],statKreat[,2:3],statBili[,2:3],statHb[,2:3],statHE[,2:3],statMAP[,2:3],statAST[,2:3],statLact[,2:3])
agg7$status <- 1
chodzi mi o to zeby zrobic taka funkcje ktora policzy to automatycznie dla roznych timeMax i wyniki za kazdym razem zapisze w innym agg[timeMax]
mam nadzieje ze to jakos wyjasnilam lepiej, mam nadzieje ze bedziesz mogl mi pomoc, z gory wielkie dzieki