Steve Einbender
2013-Feb-24 14:39 UTC
[R] Use of the newdata parameter in the predict.coxph function
Hello, I've executed the following predict.coxph function to enable prediction for new variable values (error is included). *predict(cox_out,newdata=data.frame(Meter3.Value=100.001, Meter4.Value=200.001,Meter5.Value=300.001,Meter10.Value400.001,type="expected")) Error in model.frame.default(data = data.frame(Meter3.Value = 100.001, : variable lengths differ (found for 'Meter3.Value') 6: model.frame.default(data = data.frame(Meter3.Value = 100.001, Meter4.Value = 200.001, Meter5.Value = 300.001, Meter10.Value 400.001, type = "expected"), formula = ads1S ~ Meter3.Value + Meter4.Value + Meter5.Value + Meter10.Value, na.action = function (object, ...) object)* I feel as though I've exhaustively researched the "variable length" problem, that is: Meter3.Value is Double, Numeric in both the original dataframe that created the cox_out object (i.e., type: list class:coxph) and Double, Numeric in the newdata dataframe below. Thanks for taking a look at this. Steve I've included all the code and two datasets at this location in Dropbox: https://www.dropbox.com/sh/2m51tl4l60iq9ru/0DkPYA1E-c 1. R_code_for_coxph.txt is the set of R functions used in the analysis 2. final_sheet_v3_2.xlsx is the input data set 3. cox_out.R is the coxph object I'm also curious if anyone has had any experience with the collapse parameter in predict(). Any insight here would be very helpful. I'm using the same data as above. I'm assuming, based on the documentation, that collapse is just a vector of the subject ID's to aggregate the hazard function by subject ID but it's not working. I created a simple dataframe called collapse: ASSET_ID 1 260 2 263 but the predict function is complaining about one of the covariates: *predict(cox_out,type="expected",collapse)* *Error in eval(expr, envir, enclos) : object 'Meter3.Value' not found* [[alternative HTML version deleted]]
Andrews, Chris
2013-Feb-25 12:15 UTC
[R] Use of the newdata parameter in the predict.coxph function
Couldn't reproduce your error (but I did get a warning message):> ads1S <- with(ads1,Surv(INTERVAL_START,EVENT,STATUS,type=c('counting')))Warning message: In Surv(INTERVAL_START, EVENT, STATUS, type = c("counting")) : Stop time must be > start time, NA created> cox_out <- coxph(ads1S~Meter3.Value + Meter4.Value + Meter5.Value + Meter10.Value, data=ads1, model=TRUE, x=TRUE, y=TRUE ) > predict(cox_out)[1] -1.19774097 -1.47279824 -0.76264670 -0.89077702 -0.31121008 -0.31378214 -0.33851989 -0.41849240 -0.59889048 -0.27402152 -0.82761338 [12] -0.79388564 -1.02774234 -1.12108786 -1.39410460 -0.02585448 1.04098785 1.09806886 0.11804219 0.85303783 0.54535465 0.37521077 [23] 0.35958454 0.46680146 0.40568869 1.15677782 -1.05489122 -0.69323263 1.11558766 0.44046102 -0.51461595 -0.16067974 -0.44230767 [34] -0.08525554 0.08454627 -0.14084451 -0.12752372 0.26456751 -0.22514447 -0.23150850 0.25184530 0.39600332 0.21792620 -0.11326854 [45] -0.25130227 0.24762887 0.71834917 -0.12449250 0.44837962 0.28646581 0.35451803 -0.05495787 0.03494455 0.29297170 0.45655617 [56] 0.95394560 1.93318616 0.53368463 0.46502719 0.51133378 0.24489253 -0.68318291> predict(cox_out,type="expected")1 2 3 4 5 7 8 9 10 11 12 13 14 0.8987255 0.3895238 0.2455976 4.6584416 1.3184868 1.9914932 0.9221335 0.4582996 0.7141309 0.3724664 2.3198246 0.4748342 0.7120890 15 17 18 20 21 23 24 26 28 29 30 31 32 0.5367001 0.1626016 0.7634858 0.9036739 0.9085294 0.7884793 0.8860263 1.7337697 1.7277863 1.5317648 0.7953659 0.7852396 0.8857003 34 36 37 38 39 40 41 42 43 44 45 46 47 0.4590633 0.5492261 0.8814637 0.7910453 0.5929469 0.6748266 0.6102752 0.6911543 0.7261793 0.6791640 0.6820596 1.4005810 0.5215031 48 49 50 51 52 53 54 55 56 57 58 59 61 1.0404717 0.6377536 0.6703538 0.6298815 1.1060371 1.6495236 2.0729627 0.8243298 0.6688793 0.7817669 0.7528888 0.7653308 0.6840951 62 63 64 65 66 67 68 69 70 71 0.7031948 1.5018867 1.5925973 0.8788610 0.9507775 0.8265580 1.6465624 0.8365011 1.6341284 0.0000000> predict(cox_out,newdata=data.frame(Meter3.Value=100.001,+ Meter4.Value=200.001,Meter5.Value=300.001,Meter10.Value+ 400.001,type="expected")) 1 12.14023 -----Original Message----- From: Steve Einbender [mailto:steve.einbender at gmail.com] Sent: Sunday, February 24, 2013 9:40 AM To: r-help at r-project.org Subject: [R] Use of the newdata parameter in the predict.coxph function Hello, I've executed the following predict.coxph function to enable prediction for new variable values (error is included). *predict(cox_out,newdata=data.frame(Meter3.Value=100.001, Meter4.Value=200.001,Meter5.Value=300.001,Meter10.Value400.001,type="expected")) Error in model.frame.default(data = data.frame(Meter3.Value = 100.001, : variable lengths differ (found for 'Meter3.Value') 6: model.frame.default(data = data.frame(Meter3.Value = 100.001, Meter4.Value = 200.001, Meter5.Value = 300.001, Meter10.Value = 400.001, type = "expected"), formula = ads1S ~ Meter3.Value + Meter4.Value + Meter5.Value + Meter10.Value, na.action = function (object, ...) object)* I feel as though I've exhaustively researched the "variable length" problem, that is: Meter3.Value is Double, Numeric in both the original dataframe that created the cox_out object (i.e., type: list class:coxph) and Double, Numeric in the newdata dataframe below. Thanks for taking a look at this. Steve I've included all the code and two datasets at this location in Dropbox: https://www.dropbox.com/sh/2m51tl4l60iq9ru/0DkPYA1E-c 1. R_code_for_coxph.txt is the set of R functions used in the analysis 2. final_sheet_v3_2.xlsx is the input data set 3. cox_out.R is the coxph object I'm also curious if anyone has had any experience with the collapse parameter in predict(). Any insight here would be very helpful. I'm using the same data as above. I'm assuming, based on the documentation, that collapse is just a vector of the subject ID's to aggregate the hazard function by subject ID but it's not working. I created a simple dataframe called collapse: ASSET_ID 1 260 2 263 but the predict function is complaining about one of the covariates: *predict(cox_out,type="expected",collapse)* *Error in eval(expr, envir, enclos) : object 'Meter3.Value' not found* [[alternative HTML version deleted]] ********************************************************** Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues