• 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 MIXED Equivalent in Python for Least Squared Means ANOVA

PROC MIXED Equivalent in Python for Least Squared Means ANOVA

January 8, 2021 Leave a Comment

When working with data as a data science or data analyst, ANOVA is very common and something that many industries and companies utilize to compare the means of two distinct populations.

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 performing least means squared ANOVA in SAS is the PROC MIXED procedure. In this article, you’ll learn the Python equivalent of PROC MIXED for Least Means Squared ANOVA.

PROC MIXED Equivalent in Python for Least Squared Means ANOVA

Doing least squared means ANOVA in Python is very straightforward. All it takes is a few lines of code and you can fit your ANOVA model.

We will use the statsmodels Package to fit our regression models and get the least squared means ANOVA results.

Let’s say we have data like the following, made up of some categorical and numeric data:

In SAS, to do a least squared means ANOVA, we would do something like the following:

The code above produces the following results:

proc-mixed-output

To get the same results in Python, you can do the following with the statsmodels package:

import pandas as pd
import numpy as np
import statsmodels.api as sm
from statsmodels.formula.api import ols

model = 'height ~ C(type)'
anova = sm.stats.anova_lm(ols(model,data=data).fit(),type=2)
print(anova)

#output:
#            df      sum_sq     mean_sq         F    PR(>F)
#C(type)    1.0  266.944444  266.944444  5.540133  0.034981
#Residual  13.0  626.388889   48.183761       NaN       NaN

print(ex.groupby("type")["height"].describe())
#output: 
#      count       mean       std   min    25%   50%    75%   max
#type
#Cat     9.0  23.888889  4.859127  15.0  20.00  25.0  25.00  30.0
#Dog     6.0  32.500000  9.354143  20.0  26.25  32.5  38.75  45.0

We can see here that the results are the same as SAS.

I hope that this article has been useful for you in trying to get the Python equivalent of PROC MIXED.

Other Articles You'll Also Like:

  • 1.  Get Year from Date in pandas DataFrame
  • 2.  Using Python to Create List of Prime Numbers
  • 3.  Using Python to Print Plus or Minus Sign Symbol
  • 4.  Check if All Elements in Array are Equal in Python
  • 5.  Count Spaces in String in Python
  • 6.  Read Pickle Files with pandas read_pickle Function
  • 7.  pandas set_value – Using at() Function to Set a Value in DataFrame
  • 8.  Perfect Numbers in Python
  • 9.  pandas covariance – Calculate Covariance Matrix Using cov() Function
  • 10.  How to Split a String in Half Using Python

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