[R] Help for a function
Rui Barradas
ruipbarradas at sapo.pt
Thu Aug 29 17:32:39 CEST 2013
Hello,
You should post your questions to r-help at r-project.org, the odds of
getting more and better answers are greater.
As for the question, try the following. Note that the functions now have
an extra argument.
incub <- function(x, n = 2){
x$Incubation <- 0
x$Incubation[1] <- x$Symptomes[1]
if(nrow(x) >= n)
x$Incubation[2] <- sum(x$Symptomes[seq_len(n)])
for(i in seq_len(nrow(x))[-seq_len(n)])
x$Incubation[i] <- sum(x$Symptomes[i - (seq_len(n) - 1)])
x
}
contag <- function(x, n = 7){
x$CONTAGIEUX <- 0
for(i in 1:min(nrow(x), n))
x$CONTAGIEUX[i] <- sum(x$Symptomes[1:i], na.rm = TRUE)
for (i in seq_len(nrow(x))[-seq_len(n)]) {
x$CONTAGIEUX[i] <- x$Symptomes[i] + x$CONTAGIEUX[i-1] -
x$Symptomes[i-n]
}
x
}
incub_ARGENTINA <-incub(ARGENTINA, 2)
incub_ARGENTINA
contag_ARGENTINA <-contag(ARGENTINA, 7)
contag_ARGENTINA
derdata_ARGENTINA <-merge(contag_ARGENTINA, incub_ARGENTINA)
derdata_ARGENTINA
Hope this helps,
Rui Barradas
Em 29-08-2013 08:31, teko maurice escreveu:
>
>
> Dear Rui,
> Long time!!!!!
> I came to ask for advice and help if you have time.
> I am on my PHD developping all to model pandemic.
> I have post on R help but nobody answer me,maybe it's so specific.
> So i back to you if you can help me.
> Hello all,
> I have such a datasets for a pandemic virus.
> DATE Algeria Antigua.and.Barbuda ARGENTINA AUSTRALIA AUSTRIA Bahamas
> 1 2009-04-24 0 0 0 0 0 0
> 2 2009-04-26 0 0 0 0 0 0
> 3 2009-04-27 0 0 0 0 0 0
> 4 2009-04-28 0 0 0 0 0 0
> 5 2009-04-29 0 0 0 1 0 0
> 6 2009-04-30 0 0 0 1 0 0
> 7 2009-05-01 0 0 0 1 0 0
> 8 2009-05-02 0 0 0 1 0 0
> 9 2009-05-03 0 0 0 1 0 0
> 10 2009-05-04 0 0 0 1 0 0
> 11 2009-05-05 0 0 0 1 0 0
> 12 2009-05-06 0 0 0 1 0 0
> 13 2009-05-07 0 0 0 1 0 0
> 14 2009-05-08 0 0 0 1 0 0
> 15 2009-05-09 0 0 1 2 0 0
> 16 2009-05-10 0 0 1 2 0 0
> 17 2009-05-11 0 0 1 1 1 0
> 18 2009-05-12 0 0 1 1 1 0
> 19 2009-05-13 0 0 1 1 1 0
> 20 2009-05-14 0 0 1 1 1 0
> 21 2009-05-15 0 0 1 1 1 0
> 22 2009-05-16 0 0 1 1 1 0
> 23 2009-05-17 0 0 1 1 1 0
> 24 2009-05-18 0 0 1 1 1 0
> 25 2009-05-19 0 0 1 1 1 0
> 26 2009-05-20 0 0 1 1 1 0
> 27 2009-05-21 0 0 1 3 1 0
> 28 2009-05-22 0 0 1 7 1 0
> 29 2009-05-23 0 0 1 12 1 0
> 30 2009-05-25 0 0 2 16 1 0
> 31 2009-05-26 0 0 5 19 1 0
> 32 2009-05-27 0 0 19 39 1 0
> 33 2009-05-29 0 0 37 147 1 0
> 34 2009-06-01 0 0 100 297 1 1
> 35 2009-06-03 0 0 131 501 1 1
> 36 2009-06-05 0 0 147 876 2 1
> 37 2009-06-08 0 0 202 1051 5 1
> 38 2009-06-10 0 0 235 1224 5 2
> 39 2009-06-11 0 0 256 1307 7 1
> 40 2009-06-12 0 0 343 1307 7 1
> 41 2009-06-15 0 0 343 1823 7 1
> 42 2009-06-17 0 0 733 2112 7 2
> 43 2009-06-19 0 0 918 2199 8 2
> 44 2009-06-22 1 0 1010 2436 9 2
> 45 2009-06-24 3 2 1213 2857 12 6
> 46 2009-06-26 2 2 1391 3280 12 4
> 47 2009-06-29 2 2 1488 4038 12 4
> 48 2009-07-01 2 2 1587 4090 15 6
> 49 2009-07-03 5 2 1587 4568 15 6
> 50 2009-07-06 5 2 2485 5298 19 7
>
> ...
> # I transform the date in numeric
> t1<-derdata[["DATE"]]
> t <- as.numeric(t1)
> t
> #i add the numeric date to the dataset
> Mydata<-data.frame(derdata,t)
> Mydata
> # I select each country
> ARGENTINA<- subset(Mydata,select = c(DATE,t,Canada))
> names(ARGENTINA)[3] <- "Symptomes"
> ARGENTINA
>
> ##### I create two functions because i want to create two variables by country :Incubation and CONTAGIEUX
>
> ##### 1. Here is the incubation function (a person getting a virus ,incub the virus for a certain time : 2 days)
> incub <- function(x){
> x$Incubation <- 0
> x[order(x$t),]
> x$Incubation[1] <- x$Symptomes[1]
> if(nrow(x) > 1)
> x$Incubation[2] <- sum(x$Symptomes[1:2])
> for(i in seq_len(nrow(x))[-(1:2)])
> x$Incubation[i] <- sum(x$Symptomes[i - (0:1)])
> x
> }
>
> 2. Contagion function (after incubing the virus, the subject can spread now the virus for the 7 coming days)
>
> contag <- function(x){
> x$CONTAGIEUX <- 0
> for(i in 1:min(nrow(x), 7))
> x$CONTAGIEUX[i] <- sum(x$Symptomes[1:i], na.rm = TRUE)
> for (i in seq_len(nrow(x))[-(1:7)]) {
> x$CONTAGIEUX[i] <- x$Symptomes[i] + x$CONTAGIEUX[i-1] -
> x$Symptomes[i-7]
> }
> x
> }
>
> # and So i obtain by country what i need.
> incub_ARGENTINA <-incub(ARGENTINA)
> incub_ARGENTINA
> contag_ARGENTINA <-contag(ARGENTINA)
> contag_ARGENTINA
> derdata_ARGENTINA <-merge(contag_ARGENTINA, incub_ARGENTINA)
> derdata_ARGENTINA
>
>
>
> Now my problem is that i want to update those functions incub and contag so that i can easily change the incubation and contagion period.
> they will not be fixe.
> the incubation period can be 2 or 1 or 3 or 4 or 5 and so.
> also the contagion period can be 4 or 5 or 6 or 8 or 9,......
> And i want to not select the country before launch the the two funcions (incub and contag)
> so that i have all the datasets for all country in the same dataset
>
> Maurice
>
>
>
>
>
>
>
>
>
> ________________________________
> De : Rui Barradas <ruipbarradas at sapo.pt>
> À : Jim Lemon <jim at bitwrit.com.au>
> Cc : anoumou <teko_maurice at yahoo.fr>; r-help at r-project.org
> Envoyé le : Mercredi 5 décembre 2012 16h26
> Objet : Re: [R] Help for a function
>
>
> Hello,
>
> Also, t1 and min(xt) do not vary inside the loop so if it enters the
> loop it never exits.
> And
>
> res1[j] <-(a*h)
> res2 <-sum( res1[j])
>
> is equivalent to
>
> res2 <- a*h
>
> so the inner-most loop is not needed at all.
>
> Hope this helps,
>
> Rui Barradas
> Em 05-12-2012 04:20, Jim Lemon escreveu:
>> On 12/05/2012 01:01 AM, anoumou wrote:
>>> Hello all,
>>> I need a help.
>>> I am modeling a disease and a create a R function like that:
>>> ...
>>> But i do not get the results,i try by all means but i d'ont
>>> understant the
>>> problem.
>>
>> Hi anoumou,
>> Your function provides almost no indication of what two of its five
>> arguments are supposed to be. If, with a certain degree of optimistic
>> inference, we suppose "x" to be a data frame organized as shown below
>> the function. "t", "i" and "CONTAGIEUX" must be the appropriately
>> named columns of the data frame. This leaves two columns, "Symptomes"
>> and "Incubation". Say we flip a coin to decide which of these to
>> assign to "r", and with all of our degrees of freedom gone, we assume
>> that the other is "h". We are forced to the conclusion that "a" is a
>> nuisance argument, added to throw us off the scent.
>>
>> Peering within the function, we notice that the date format is wrong,
>> braces are unmatched and that our data frame is alphabetically ordered
>> by name of country to no purpose whatsoever. The best I can do here is
>> to make the function potentially able to do something if you can work
>> out what to do with it.
>>
>> Lambda<-function (x,date1,r,h,a) {
>> ndate1 <- as.Date(date1, "%Y-%m-%d")
>> t1 <- as.numeric(ndate1)
>> x[order(x$i),]
>> xt <-x[,"t"]
>> xi <-x[,"i"]
>> CONTAGIEUX <-x[,"CONTAGIEUX"]
>> while ( t1 < min(xt) ){
>> for (i in 1:length(xi) ){
>> for (j in 1:CONTAGIEUX[length(CONTAGIEUX)]){
>> res1[j] <-(a*h)
>> res2 <-sum( res1[j])
>> }
>> }
>> lambda[i] <- r*res2
>> }
>> x<-data.frame(x,lambda)
>> x
>> }
>>
>> Jim
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list