IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

R Discussion :

Appel d'une fonction


Sujet :

R

  1. #1
    Futur Membre du Club
    Femme Profil pro
    Data Analyst
    Inscrit en
    Février 2013
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Danemark

    Informations professionnelles :
    Activité : Data Analyst
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Février 2013
    Messages : 6
    Points : 6
    Points
    6
    Par défaut Appel d'une fonction
    Bonjour,

    J'espère vraiment que quelqu'un va pouvoir m'aider, je travaille sur des simulations de sous-échantillonage pour des données de survie et voici mon problème : j'utilise une fonction "sim" qui appelle une fonction "arms" (crée 2 groupes de survie) dans une boucle. Lorsque je lance la fonction "arms" hors de la fonction, elle marche très bien. Mais lorsque je lance la fonction la variable à laquelle j'attribue les résultst de la fonction "arms" n'est pas crée. Voici le code de la fonction "arms" :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
     
     
    library(splines)
    library(survival)
     
    # r = percentage of patients with treatment 1 (between 0 and 1)
    # N = number of patients
     
    arms <- function(r,N){
     
    	y1 <- 0.9 * rexp(ceiling(r*N), rate = 1/4)
    	y2 <- 1.1 * rexp(ceiling((1-r)*N), rate = 1/2)
     
    	time	<- c(y1, y2)
    	tmax <- max(time)
    	n1 <- length(y1)
    	n2 <- length(y2)
     
    	treat1 <- rep(0, n1)
    	treat2 <- rep(1, n2)
    	treat <- c(treat1, treat2)
     
    	censtimes1 <- 16 * runif(n1)			# censoring time vector
    	ztimes1 <- pmin( y1, censtimes1 )			# choose which comes first
    	status1 <- as.numeric ( censtimes1 > y1 )		# related indicator
     
    	censtimes2 <- 11 * runif(n2)			# censoring time vector
    	ztimes2 <- pmin( y2, censtimes2 )			# choose which comes first
    	status2 <- as.numeric ( censtimes2 > y2 )		# related indicator
     
    	status <- c(status1, status2)
     
    	samp <- cbind(time, status, treat)
    	samp <- data.frame(samp)
     
    	fit <- survfit(Surv(time, status) ~ treat, data = samp)
    	fitnocens <- survfit(Surv(time, status) ~ treat, data = samp[samp$status==1,])
    	#plot(fit, col = c(1,3))
    	#legend("topright",legend = c("T1","T2"), col = c("black","green"), pch = 15)
     
    	l <- list(y1=y1,y2=y2,tmax=tmax,n1=n1,n2=n2, fit=fit, time=time, status=status, treat=treat, samp=samp, fitnocens=fitnocens)
    	return(l)
    }
    et la fonction "sim", l'étape qui me bloque est en rouge (au début de la fonction)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    
    #nb = number of simulations
    #N = number of patients in the sample
    #r = percentage (between 0 and 1) of patients in the 1st treatment
    #p1 and p2 = percentages (between 0 and 1) of patients in each treatment in the subsample out of the sample
    #subsamp = choose the subsamp1 : "subsample" or 2 : "halfsubsample"
    #cens = 0 if no censored data are required in the subsample, 1 otherwise, 
    #this option only works for the second subsample (but is required in the "sim" function)
    sim <- function(nb, N, r, p1, p2, subsamp, cens){
    
    	coxsample <- rep(NA, nb)
    	coxsubsample <- rep(NA, nb)
    
    	for (i in 1:nb){
    
    		mainsample <- arms(r,N)
    		subsample <- subsamp( p1, p2, cens)	
    
    		coxsample[i] <- summary(coxph(Surv(mainsample$time, mainsample$status) ~ mainsample$treat))[[7]][[1]] 
    		coxsubsample[i] <- summary(coxph(Surv(subsample$time, subsample$status) ~ subsample$treat))[[7]][[1]]
    	}
    
    	diff <- coxsample - coxsubsample
    	coeffs <- cbind(coxsample ,coxsubsample,diff)
    
    	densample <- density(coeffs[,1])
    	densubsample <- density(coeffs[,2])
    	densdiff <- density(coeffs[,3])
    	
    	X11(width=10, height=8)
    	par(mfrow=c(2,2))
    	plot( densample , col = "green", main = "Coefficient values from the Cox model")
    	lines( densubsample )
    	legend("topleft",legend = c("sample","subsample"), col = c("black","green"), pch = 15)
    
    	hist(coeffs[,3], main = "Sample minus Subsample Coefficients", xlab="Coef Diff", col = "lightblue")
    	rug(coeffs[,3])
    	par(new=TRUE)
    	plot(densdiff$x, densdiff$y, type="l", axes = FALSE, ylab="",xlab="", col = "red" )
    	axis(4)
    	
    	boxplot(coeffs[,3], horizontal = TRUE, col = "lightblue", main = " Difference of coefficients - Summary ")
    	
    	X11(width=10, height=8)
    	par(mfrow=c(2,1))
    	plot(mainsample$fit, col = c(1,3), main = "Mainsample")
    	legend("topright",legend = c("sample","subsample"), col = c("black","green"), pch = 15)
    
    	if(cens == 1){
    		plot(mainsample$fit, col = c(1,3), main = "Mainsample and subsample chosen")
    		lines(subsample$fit2, col = c(1,3),lty = 2)
    		legend("topright",legend = c("T1","T2"), col = c("black","green"), pch = 15)
    	}
    
    	if(cens == 0){
    		plot(mainsample$fitnocens, col = c(1,3))
    		lines(subsample$fit2, col = c(1,3),lty = 2)
    		legend("topright",legend = c("T1","T2"), col = c("black","green"), pch = 15)
    	}
    
    		### Summary
    
    	summary(coeffs[,3])
    	#l <- list(mainsample=mainsample, subsample=subsample, coeffs=coeffs)
    	#return(l)
    }
    fonction subsampling pour l'example
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
    subsampling <- function(p1, p2, cens){
     
    	cens <- 0
     
    	mainsample1 <- cbind(mainsample$y1, mainsample$status[1:mainsample$n1], mainsample$treat[1:mainsample$n1])
    	colnames(mainsample1) <- c("time","status","treat")
    	mainsample1 <- data.frame(mainsample1)
     
    	mainsample2 <- cbind(mainsample$y2, mainsample$status[mainsample$n1+1:mainsample$n2], mainsample$treat[mainsample$n1+1:mainsample$n2])
    	colnames(mainsample2) <- c("time","status","treat")
    	mainsample2 <- data.frame(mainsample2)
     
    	y1tild <- mainsample1[sample(1:mainsample$n1,ceiling(p1*mainsample$n1), replace=FALSE),]
    	y2tild <- mainsample2[sample(1:mainsample$n2,ceiling(p2*mainsample$n2), replace=FALSE),]
     
    	n1 <- length(y1tild )
    	n2 <- length(y2tild )
     
    	ytild <- rbind(y1tild, y2tild)
     
    	fit2 <- survfit(Surv(time, status) ~ treat, data=ytild)
    	#plot(fit2, col = c(1,3))
    	#legend("topleft",legend = c("T1","T2"), col = c("black","green"), pch = 15)
     
    	l <- list(y1tild=y1tild, y2tild=y2tild, fit2=fit2, time=ytild$time, status=ytild$status, treat=ytild$treat,n1=n1, n2=n2)
    	return(l)
     
    }
    pour appeler la fonction :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    sim(nb = 500,N = 600,r = 0.7,p1 = 0.6,p2 = 0.6, subsamp = subsampling, cens = 0)
    Faites moi savoir si je ne suis pas assez claire ou s'il y a besoin de précisions.
    Merci d'avance

  2. #2
    Futur Membre du Club
    Femme Profil pro
    Data Analyst
    Inscrit en
    Février 2013
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Danemark

    Informations professionnelles :
    Activité : Data Analyst
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Février 2013
    Messages : 6
    Points : 6
    Points
    6
    Par défaut
    C'est bon problème résolu. Il fallait ajouter l'attribut "mainsample" à la fonctio"subsampling".

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. appel d'une fonction à partir d'un popup
    Par villeneuvejsp dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 02/12/2004, 17h00
  2. Appel d'une fonction
    Par georgeabitbol dans le forum ASP
    Réponses: 4
    Dernier message: 08/07/2004, 14h29
  3. Réponses: 4
    Dernier message: 02/06/2004, 16h35
  4. Appel d'une fonction
    Par jfphan dans le forum ASP
    Réponses: 4
    Dernier message: 14/04/2004, 15h06
  5. A la recherche de l'appel d'une fonction...
    Par karl3i dans le forum C
    Réponses: 3
    Dernier message: 24/09/2003, 12h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo