簡體   English   中英

如何在R中進行條件Logistic回歸的患者數據匹配?

[英]How do I match patient data for conditional logistic regression in R?

我有一個數據集,如下所示:

 patient_id   pre.int.outcome post.int.outcome 
  302949               1            1            
  993564               0            1           
  993570               1            1   
  993575               0            1    
  993792               1            0    

我想為每位患者進行clogit干預前后

我了解我需要將其整理成表格:

  strata            outcome
   1                 1
   1                 1
   2                 0
   2                 0
   3                 0
   3                 1

在這種形式中,層次是成對的患者編號和結果,但是我不確定如何做到。 任何人都可以提供幫助或定向到可以提供幫助的來源嗎?

編輯:我最終要做的是使用reshape函數使數據集“長”而不是“寬”;

    ds1<-reshape(ds, varying=c('pre.int.outcome','post.int.outcome'), v.names='outcome', timevar='before_after', times=c(0,1), direction='long')

我按Patient_id排序,以將其用作我的“階層”。

    ds1[order(ds1$patient_id),]

也許這會有所幫助

data.frame(strata= rep(1:nrow(df1), each=2), outcome=c(t(df1[2:3])))

基於akrun的評論和答案,這是使用reshape2包的melt的解決方案:

library(reshape2)

# I created dummy data to make sure my answer works
# I assumed 4 intervention treatments, but this would work with 
# two treatments. With the dummy data, just make sure nObs/4 is an integer
nObs = 100 # number of observations
d = data.frame(patient_id = 1:4, 
           pre.int.outcome = rbinom(4, 1, 0.7), 
           post.int.outcome = rbinom(4, 1, 0.5),
           intervention = rep(c("a", "b", "c", "d"), each = nObs/4))
# melting the data as suggested by akrun  
d2 = melt(d, id.vars =  c("patient_id", "intervention"))

# Creating a strata variable for you with paste
d2$strata = as.factor(paste(d2$patient_id, d2$variable))
# I also clean up the variable to remove patient_id
# useful if you are concerned about protecting pii
levels(d2$strata) = 1:length(d2$strata)
# last, I clean up the data and create a third "pretty" data.frame
d3 = d2[ , c("intervention", "value", "strata")]
head(d3)
# intervention value strata
# 1            a     1      2
# 2            a     1      4
# 3            a     1      6
# 4            a     1      8
# 5            a     1      2
# 6            a     1      4
# I also throw in the logistic regression
myGLM = glm(value ~ intervention, data = d3, family = 'binomial')
summary(myGLM)
# prints lots of outputs to screen ...

# or if you need odds ratios
myGLM2 = glm(value ~ intervention - 1, data = d3, family = 'binomial')
exp(myGLM2$coef)
exp(confint(myGLM2))
# also prints lots of outputs to screen ...

編輯:我根據OP的評論進行了intervention 我還添加了glm ,以進一步幫助他或她。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM