本文介绍了用R绘制cox回归的预测变量的二次关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要在cox回归中绘制二次效应的相对风险。我的模型如下所示:I need to plot the relative risk for a quadratic effect in a cox regression. My model looks like this:cox_mod <- coxph(Surv(time, status) ~ ph.karno + pat.karno + meal.cal + meal.cal_q, data = lung)其中food.cal_q定义为:Where meal.cal_q is defined as:lung$meal.cal_q <- lung$meal.cal^2该图应考虑meat.cal和meat.cal_q的系数,并在y轴上显示相对风险X轴上的进餐量值。相对风险应定义为与所有预测指标处于均值相比,给定进餐量值的风险。另外,该图应包括95%的置信区间。该图应如下所示: 预期的图 The plot should consider the coefficients of meal.cal and meal.cal_q and show the relative risk on the y-axis and the meal.cal values on the x-axis. The relative risk should be defined as the risk at a given meal.cal value compared to all of the predictors being at their mean. Additionaly, the plot should include the 95% confidence intervals. The plot should look something like this:Expected plot如果可能,该图应该是ggplot对象,以便我可以自定义它。If possible, the plot should be a ggplot object so that I can customize it.我已经阅读了数小时网络上,但无法弄清楚所描述的情节并希望有人可以帮助我。例如,我使用predict()函数进行了尝试:I have been reading for hours on the web, but can not figure out how make the described plot and hope someone can help me. I tried it for example with the predict() function:meal.cal_new <- seq(min(lung$meal.cal, na.rm= TRUE), max(lung$meal.cal, na.rm= TRUE), by= 1)meal.cal_q_new <- meal.cal_new^2n <- length(meal.cal_new)lung_new <- data.frame(ph.karno= rep(mean(lung$ph.karno, na.rm= TRUE), n), pat.karno= rep(mean(lung$pat.karno, na.rm= TRUE), n), meal.cal= meal.cal_new, meal.cal_q = meal.cal_q_new)predicted_rel_risk <- predict(cox_mod, lung_new, interval = "confidence")print(predicted_rel_risk)首先,预测值不包括95%置信度迭代。此外,predicted_rel_risk中存在负值,在我看来,这是不应该的,因为最小相对风险应该为零。 因此,我无法获得所需的绘图。所以我所能做的就是这样:Firstly, the predicted values do not include the 95% confidence itnervals. And in addition there are negative values in predicted_rel_risk which in my opinien should not be the case since the minimal relative risk should be zero.Therefore I can not get the desired plot. So all I can do is this:lung_new$predicted_rel_risk <- predicted_rel_riskggplot(lung_new, aes(meal.cal, predicted_rel_risk)) +geom_smooth(se= TRUE)结果图不包括置信区间,并且显示出负的相对风险。这是我得到的:The resulting plot does not include the confidence intervals and shows neagtive relative risk. Here is what I get: 谢谢推荐答案该预测包括负值,因为您没有指定要获得相对风险(因为您说)。尝试以下代码The prediction includes negative values since you did not specify that you want to obtain the relative risk (as you stated). Try the following codepredicted_rel_risk <- predict(cox_mod, lung_new, interval = "confidence",type= "risk")这将为您提供以下情节:This gives you the following plot: 无负值的图 为了同样获得置信区间,可以使用自举。简而言之,这意味着将从您的数据中抽取一个随机样本并计算相对风险。例如,此过程将重复10,000次。这将为您的预测变量的每个值提供10,000个不同的相对风险值。您可以通过计算预测变量每个值的平均相对风险来获得绘图的主线。要获得置信区间,您需要对预测变量的每个值从最小到最大排序相对风险。第250(9,750)个相对风险值为您提供了较低的(较高)ci。再次,它是每个预测值的第250个(第9,750个)值。In order to get the confidence intervalls as well, you can use bootstrapping. To put it short, this means that a random sample will be drawn from your data and the relative risk will be calculated. This procedure will be repeated 10,000 times, for example. This gives you 10,000 different relative risk values for each value of your predictor. You get the main line for your plot by calculating the mean relative risk for each value of your predictor. To get the condidence intervall, you need to order the relative risks from the smallest to the greatest for each value of your predictor. The 250th (9,750th) relative risk value gives you your lower (upper) ci. Again, it is the 250th (9,750th) value of each predictor value.希望这会有所帮助。 这篇关于用R绘制cox回归的预测变量的二次关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-22 07:53