David Lloyd
2007-Sep-10 17:02 UTC
[R] Loop and loop output [Cox model, for, function, loglik]
Dear R users, Below I have written 4 functions CIT1, CIT2a and CIT2b and CIT3 which recode a variable CLD_ISCH into 3 new variables(T1 T2 T3), I wish to use T1, T2 and T3 based on the values of tf1 and tf2. (NOTE:- T2a is used to create T2 in a long winded manner due to my lack of programming experience) I then attach T1 T2 and T3 to a dataset KidneyT that contains other variables i wish to use in a Cox regression model. I require the log likelihood statistic of a Cox model when variables T1 T2 T3 and some from KidneyT are fitted. ( coxmodel$loglik[2] ) ######################################################################## ########## I can get this to work fine for specified values of tf1 and tf2, but wish to get the loglikelihood value for many combinations of tf1 and tf2 which are times from 1 to 50 hours i.e I need loglik statistic when tf1 =1 and tf2 = 1 to 50, tf1=2 and tf2=1 to 50. I'd like to do this in increments of 0.5 hours. Ideally I'd like to end up with a matrix/array like tf1 tf2 loglik 1 0.5 -1200 1 1 -1250 1 1.5 -1251 2 0.5 -1252 2 1 -1249 2 1.5 -1159 etc. In the hope of finding a maximum for loglik. ######################################################################## ########## I've looked into loops and apply and many other functions but can't seem to get what i want. Any suggestions would be fantastic - else I'll be doing hundreds of calculations 1 at a time - ahhhhh! Code is below: - sorry for not including an example dataset. Am hoping the code itself. If not pls say and i'll try and repost with a small example dataset. ######################################################### tf1=11 tf2=18 CIT1=function(var1,bp1){ { ifelse (var1<=bp1,(var1=var1),(bp1)) } } T1=CIT1(CLD_ISCH,tf1) #----------------------------------------------------------------------- -------# CIT2a=function(var1,var2,bp1){ { ifelse (var1<=bp1,(var1=0),(var1=var2-bp1)) } } T2a=CIT2a(CLD_ISCH,CLD_ISCH,tf1) #----------------------------------------------------------------------- -------# CIT2b=function(var1,var2,bp1,bp2){ { ifelse (var1>(bp2-bp1),(var1=bp2-bp1),(var1=T2a)) } } T2=CIT2b(T2a,T2a,tf1,tf2) #----------------------------------------------------------------------- -------# CIT3=function(var1,var2,bp2){ { ifelse (var1<=bp2,(var1=0),(var1=var2-bp2)) } } T3=CIT3(CLD_ISCH,CLD_ISCH,tf2) #----------------------------------------------------------------------- -------# #----------------------------------------------------------------------- -------# newdata=data.frame(T1,T2,T3,kidneyT) attach(newdata) coxmodel=coxph(Surv(TSURV,STATUS)~RAGE+DAGE+REG_WTIME_M+POLY_VS+T1+T2+T3 , method="breslow", data=newdata) coxmodel$loglik[2] ########################################################## Regards and Thx DaveL Buying or selling a home? Click here for free info on real estate services. <http://tagline.bidsystem.com/fc/Ioyw36XILaXQaXpqHLyCgxKj2HWmH06KAobcQpI xUGTio1SLafTDjw/> <span id=m2wTl><p><font face="Arial, Helvetica, sans-serif" size="2" style="font-size:13.5px">_______________________________________________________________<BR>Get the Free email that has everyone talking at <a href=http://www.mail2world.com target=new>http://www.mail2world.com</a><br> <font color=#999999>Unlimited Email Storage – POP3 – Calendar – SMS – Translator – Much More!</font></font></span> [[alternative HTML version deleted]]
Reasonably Related Threads
- (Censboot, Z-score, Cox) How to use Z-score as the statistic within censboot?
- (coxph, se) Obtaining standard errors of coefficients from coxph to store
- there are fontencoding problem in Sweave
- Help on Calculating day differences
- there are fontencoding problem in Sweave