0

I am unable plot a histogram of normalized data from the penguins data set.

library(bestnormalize)
data(penguins, package = "palmerpenguins")
ppp <- penguins

bestNormalize(ppp$flipper_length_mm)
ok <- orderNorm(ppp$flipper_length_mm)
newflip <- boxcox(flipper_length_mm)
hist(newflip)
## Error in hist.default(newflip) : 'x' must be numeric
Mikael Jagan
  • 9,012
  • 2
  • 17
  • 48

2 Answers2

0

The function gives a list object. The normalized data are in the list with the name x.t and can be extracted with the $ operator. Likewise the the Box-Cox transformed values are x.t values stored in the newflip list and must be passed to histogram function using $ operator- Here the code is rewritten according to the question, and to get the best answer, my advise is to read the vignette ?bestNormalize and check if other parameters should be taken into consideration when performing normalization using the package:

library(bestNormalize)

set.seed(111)
ppp<-penguins

data_norm<-bestNormalize(ppp$bill_length_mm
                         ,allow_orderNorm = T
                         )

data_norm<-data_norm$x.t
hist(data_norm)

newflip=boxcox(ppp$flipper_length_mm)

hist(newflip$x.t)

enter image description here

S-SHAAF
  • 1,863
  • 2
  • 5
  • 14
0

It's throwing an error because you're passing the newflip to the function (which is a list). As per the boxcox function's help (use ?boxcos to access function's documentation), the output of the function is a list comprising of the following:

A list of class boxcox with elements:

x.t transformed original data

x original data

... (other elements in the list)

So if you pass newflip$x.t to the hist function it should work.

Below is a more complete (reproducible) answer:

library(palmerpenguins)
library(bestNormalize)

data(penguins)
ppp <- penguins

bestNormalize(ppp$flipper_length_mm)
ok <- orderNorm(ppp$flipper_length_mm)
# There's a base function with the same name; so best 
# use :: specify the package
newflip <- bestNormalize::boxcox(ppp$flipper_length_mm)
newflip

hist(newflip$x.t)

BONUS MATERIAL

  1. In R use <- as the assignment operator. There's a subtle difference between <- and =. See this answer for details.
  2. While hist() function is ok for a quick glance, use ggplot2 for better graphs. See this document for details.
fahmy
  • 3,543
  • 31
  • 47