# Multivariatestats: No method matching fit for PCA & DataFrame

**URL:** <https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291>\
**Category:** New to Julia\
**Created:** [October 30, 2020, 7:56am UTC](https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291 "2020-10-30T07:56:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vphpin](https://avatars.discourse-cdn.com/v4/letter/v/e274bd/32.png) [@vphpin](https://discourse.julialang.org/u/vphpin)\
**Post date:** [October 30, 2020, 7:56am UTC](https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291/1 "2020-10-30T07:56:13Z")

</div>

Hi! I am currently trying to learn both ML and Julia and I am looking at this dataset, and wish to perform PCA on it. [UCI Machine Learning Repository: Zoo Data Set](https://archive.ics.uci.edu/ml/datasets/zoo)  
Reading the example in [Principal Component Analysis — MultivariateStats 0.1.0 documentation](https://multivariatestatsjl.readthedocs.io/en/stable/pca.html) I expected the following to work.

```julia
using Markdown
using InteractiveUtils
using LinearAlgebra
using Statistics
using MLJ
using CSV
using MultivariateStats
import DataFrames: DataFrame, select, Not, describe

X = CSV.read("zoo.data"; header=[:name, :hair, :feathers, :eggs, :milk, :airborne, :aquatic, :predator, :toothed, :backbone, :breathes, :venomous, :fins, :legs, :tail, :domestic, :catsize, :type])
X_type = select(X, :type)
X = select(X, Not([:type, :name]))

M = fit(PCA, X)

```

but it gives the error "ERROR: LoadError: MethodError: no method matching fit(::Type{PCA}, ::DataFrame)  
Closest candidates are:  
fit(!Matched::Type{StatsBase.Histogram}, ::Any…; kwargs…) at …  
fit(!Matched::StatsBase.StatisticalModel, ::Any…) at …  
fit(!Matched::Type{D}, ::Any) where D\<:Distributions.Distribution at …  
…  
"  
I tried converting the dataframe to an array, but it gave the same result, so I must clearly be misunderstanding something. Any ideas?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 30, 2020, 8:24am UTC](https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291/2 "2020-10-30T08:24:38Z")

</div>

Should work with an array:

```julia
julia> using DataFrames, MLJ, MultivariateStats

julia> data = DataFrame(rand(100, 5));

julia> fit(PCA, Matrix(data))
PCA(indim = 100, outdim = 4, principalratio = 1.0)

```

I also imported MLJ here since I thought MLJ exports a `fit` function as well which could have clashed, but that’s actually not the case.

---

<div class="post-metadata">

**Author:** ![vphpin](https://avatars.discourse-cdn.com/v4/letter/v/e274bd/32.png) [@vphpin](https://discourse.julialang.org/u/vphpin)\
**Post date:** [October 30, 2020, 8:56am UTC](https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291/3 "2020-10-30T08:56:13Z")

</div>

That works! But is it required to be a float? Most columns are boolean, but that gives an error.

```julia
X = iszero.(select(X, Not([:type, :name])))
fit(PCA, Matrix(X))
>>
TypeError: in typeassert, expected Array{Bool,1}, got a value of type Array{Float64,1}

```

Thank you!

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 30, 2020, 10:12am UTC](https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291/4 "2020-10-30T10:12:32Z")

</div>

Are you sure you want to perform a standard PCA on boolean data?  
You may consider something PCA-like from [https://github.com/madeleineudell/LowRankModels.jl](https://github.com/madeleineudell/LowRankModels.jl)  
for boolean data they suggest a `LogisticLoss`. They also support fitting data frames directly.

---

<div class="post-metadata">

**Author:** ![vphpin](https://avatars.discourse-cdn.com/v4/letter/v/e274bd/32.png) [@vphpin](https://discourse.julialang.org/u/vphpin)\
**Post date:** [October 30, 2020, 11:47am UTC](https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291/5 "2020-10-30T11:47:42Z")

</div>

Yes, it is part of a school assignment to use PCA on this dataset, I will use other methods later. The link looks very useful, I will give it a go tomorrow.  
Thank you!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 30, 2020, 1:10pm UTC](https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291/6 "2020-10-30T13:10:08Z")

</div>

Check out the dimensions at the end, here.

PCA treats columns as observations in a matrix, so you have to input `transpose(Matrix(data))`

---

<div class="post-metadata">

**Author:** ![vphpin](https://avatars.discourse-cdn.com/v4/letter/v/e274bd/32.png) [@vphpin](https://discourse.julialang.org/u/vphpin)\
**Post date:** [November 1, 2020, 8:21am UTC](https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291/7 "2020-11-01T08:21:56Z")

</div>

Does `transpose(Matrix(data))` work for you? I can not get it working.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 1, 2020, 3:05pm UTC](https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291/8 "2020-11-01T15:05:52Z")

</div>

Works for me:

```julia
julia> df = DataFrame(rand(100, 20));

julia> fit(PCA, transpose(Matrix(df)), maxoutdim = 5)
PCA(indim = 20, outdim = 5, principalratio = 0.4258331088614564)

```

---

<div class="post-metadata">

**Author:** ![vphpin](https://avatars.discourse-cdn.com/v4/letter/v/e274bd/32.png) [@vphpin](https://discourse.julialang.org/u/vphpin)\
**Post date:** [November 5, 2020, 8:32pm UTC](https://discourse.julialang.org/t/multivariatestats-no-method-matching-fit-for-pca-dataframe/49291/9 "2020-11-05T20:32:59Z")

</div>

Sorry, I should clarify. That works for me too, but trying to use it on the dataframe X in the original post does not.
