class_docstr: Test correlation.py.
  
  See the test_correlation.R file.
class_docstr_tok: ["Test","correlation.py",".","See","the","test_correlation.R","file","."]
docstr: Test function partial_corr.
  
  Compare with the R package ppcor (which is also used by JASP).
docstr_tok: ["Test","function","partial_corr",".","Compare","with","the","R","package","ppcor","(","which","is","also","used","by","JASP",")","."]
code: def test_partial_corr(self):
      """Test function partial_corr.
  
      Compare with the R package ppcor (which is also used by JASP).
      """
      df = read_dataset("partial_corr")
      #######################################################################
      # PARTIAL CORRELATION
      #######################################################################
      # With one covariate
      pc = partial_corr(data=df, x="x", y="y", covar="cv1")
      assert round(pc.at["pearson", "r"], 7) == 0.5681692
      assert round(pc.at["pearson", "p-val"], 9) == 0.001303059
      # With two covariates
      pc = partial_corr(data=df, x="x", y="y", covar=["cv1", "cv2"])
      assert round(pc.at["pearson", "r"], 7) == 0.5344372
      assert round(pc.at["pearson", "p-val"], 9) == 0.003392904
      # With three covariates
      # in R: pcor.test(x=df$x, y=df$y, z=df[, c("cv1", "cv2", "cv3")])
      pc = partial_corr(
          data=df, x="x", y="y", covar=["cv1", "cv2", "cv3"]
      )
      assert round(pc.at["pearson", "r"], 7) == 0.4926007
      assert round(pc.at["pearson", "p-val"], 9) == 0.009044164
      # Method == "spearman"
      pc = partial_corr(
          data=df,
          x="x",
          y="y",
          covar=["cv1", "cv2", "cv3"],
          method="spearman",
      )
      assert round(pc.at["spearman", "r"], 7) == 0.5209208
      assert round(pc.at["spearman", "p-val"], 9) == 0.005336187
  
      #######################################################################
      # SEMI-PARTIAL CORRELATION
      #######################################################################
      # With one covariate
      pc = partial_corr(data=df, x="x", y="y", y_covar="cv1")
      assert round(pc.at["pearson", "r"], 7) == 0.5670793
      assert round(pc.at["pearson", "p-val"], 9) == 0.001337718
      # With two covariates
      pc = partial_corr(data=df, x="x", y="y", y_covar=["cv1", "cv2"])
      assert round(pc.at["pearson", "r"], 7) == 0.5097489
      assert round(pc.at["pearson", "p-val"], 9) == 0.005589687
      # With three covariates
      # in R: spcor.test(x=df$x, y=df$y, z=df[, c("cv1", "cv2", "cv3")])
      pc = partial_corr(
          data=df, x="x", y="y", y_covar=["cv1", "cv2", "cv3"]
      )
      assert round(pc.at["pearson", "r"], 7) == 0.4212351
      assert round(pc.at["pearson", "p-val"], 8) == 0.02865483
      # With three covariates (x_covar)
      pc = partial_corr(
          data=df, x="x", y="y", x_covar=["cv1", "cv2", "cv3"]
      )
      assert round(pc.at["pearson", "r"], 7) == 0.4631883
      assert round(pc.at["pearson", "p-val"], 8) == 0.01496857
  
      # Method == "spearman"
      pc = partial_corr(
          data=df,
          x="x",
          y="y",
          y_covar=["cv1", "cv2", "cv3"],
          method="spearman",
      )
      assert round(pc.at["spearman", "r"], 7) == 0.4597143
      assert round(pc.at["spearman", "p-val"], 8) == 0.01584262
  
      #######################################################################
      # ERROR
      #######################################################################
      with pytest.raises(TypeError):
          # TypeError: partial_corr() got an unexpected keyword argument 'tail'
          partial_corr(data=df, x="x", y="y", covar="cv1", tail="error")
      with pytest.raises(ValueError):
          partial_corr(
              data=df, x="x", y="y", covar="cv2", x_covar="cv1"
          )
      with pytest.raises(ValueError):
          partial_corr(
              data=df, x="x", y="y", x_covar="cv2", y_covar="cv1"
          )
      with pytest.raises(AssertionError) as error_info:
          partial_corr(data=df, x="cv1", y="y", covar=["cv1", "cv2"])
      assert str(error_info.value) == "x and covar must be independent"
  
file_path: pingouin/pingouin/tests/test_correlation.py
imports: import pytest
  import numpy
  from unittest import TestCase
  from pingouin.correlation import corr, rm_corr, partial_corr, skipped, distance_corr, bicor
  from pingouin import read_dataset
label_desc: Use image node_id 2 for calling the TestCorrelation obj's underlying member method code with example usage: obj.test_partial_corr() without return types