• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / PROC PHREG Equivalent in Python

PROC PHREG Equivalent in Python

December 16, 2020 Leave a Comment

When working with data as a data science or data analyst, survival analysis is very common and something that many industries and companies utilize to understand the expected time and probabilities of some event occurring.

There are many major companies and industries which use SAS (banking, insurance, etc.), but with the rise of open source and the popularity of languages such as Python and R, these companies are exploring converting their code to Python.

A commonly used procedure for survival analysis in SAS is the PROC PHREG procedure. In this article, you’ll learn the Python equivalent of PROC PHREG.

PROC PHREG Equivalent in Python

In SAS, when we are looking at doing survival analysis on continuous variables, we use PROC PHREG. PROC PHREG performs regression analysis of survival data based on the Cox proproptional hazards model.

Let’s say we have data such as the following:

example-data-phreg

In SAS, if we wanted to fit a Cox model on this data, we could do something like the following:

proc-phreg

The output from running the code above is below:

proc-phreg-output

With an outputted dataset using the ODS output PARAMETERESTIMATES statement here:
proc-phreg-ods-pe

To get the PROC PHREG equivalent in Python, we will use the the CoxPHFitter class from the lifelines package.

Fitting a Cox model using the CoxPHFitter class is very easy.

Below gives us the equivalent output as SAS:

import pandas as pd
import numpy as np
from lifelines import CoxPHFitter

cph = CoxPHFitter()
cox1 = cph.fit(example_data, duration_col="time", event_col="event", formula="weight")
cox1.print_summary()

#output:
#<lifelines.CoxPHFitter: fitted with 40 total observations, 9 right-censored observations>
#             duration col = 'time'
#                event col = 'event'
#      baseline estimation = breslow
#   number of observations = 40
#number of events observed = 31
#   partial log-likelihood = -89.50
#         time fit was run = 2020-12-17 00:26:36 UTC
#
#---
#            coef  exp(coef)   se(coef)   coef lower 95%   coef upper 95%  exp(coef) lower 95%  exp(coef) upper 95%
#covariate
#weight      0.21       1.24       0.08             0.05             0.37                 1.05                 1.45
#
#             z    p   -log2(p)
#covariate
#weight    2.62 0.01       6.82
#---
#Concordance = 0.65
#Partial AIC = 181.00
#log-likelihood ratio test = 6.61 on 1 df
#-log2(p) of ll-ratio test = 6.63

If we want to work with the estimates like with the ODS output parameterestimates dataset, we can use the CoxPHFitter summary DataFrame as below:

print(cox1.summary)

#output:
#               coef  exp(coef)  se(coef)  coef lower 95%  coef upper 95%  exp(coef) lower 95%  exp(coef) upper 95%         z         p  -log2(p)
#covariate
#weight     0.211464   1.235485  0.080773        0.053151        0.369777             1.054589             1.447412  2.617988  0.008845  6.820924

print(cox1.summary[coef].iloc[0])

#output: 
# 0.211464

I hope that this article has been beneficial for you and helped you learn how to get the PROC PHREG equivalent in Python.

Other Articles You'll Also Like:

  • 1.  pandas min – Find Minimum Value of Series or DataFrame
  • 2.  Using Python to Replace Backslash in String
  • 3.  Fibonacci Sequence in Python with for Loop
  • 4.  Using Python To Split String by Comma into List
  • 5.  Selenium maximize_window() Function to Maximize Window in Python
  • 6.  pandas ceil – Find the Ceiling of a Column Using Numpy ceil
  • 7.  Python Get Yesterday’s Date
  • 8.  Read Last Line of File Using Python
  • 9.  How to Split a String in Half Using Python
  • 10.  Using Python to Remove Non-Empty Directory

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy