As.factor fault on inference, how do I convert to a categorical variable?


#1

Hello,

Could someone please advice how I can convert my variable to categorical using as.factor()?
I have googled the error, tried with as.factor(weeks) but it is not clear to me and I keep getting the same error.
My understanding is that I need to create more levels with the <- c(“x”…) but that seems tedious with 52 weeks.
I have enclosed a screenshot of the result I am looking for but instead of weight I want weeks.

Please find below my first reprex.
Thanks in advance!!!

reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-11-08

inference (y = weeks, data= nc, statistic = "mean", type= "ci", method= "theoretical", conf_level = 0,99)
#> Error in inference(y = weeks, data = nc, statistic = "mean", type = "ci", : could not find function "inference"
reprex::reprex_info()

reprex::reprex_info()\n#> Created by the reprex package v0.1.1.9000 on 2017-11-08\n\ninference (y = weeks, data= nc, statistic = "mean", type= "ci", method= "theoretical", conf_level = 0,99)\n#> Error in inference(y = weeks, data = nc, statistic = "mean", type = "ci", : could not find function "inference"
#> Error: <text>:3:22: unexpected input
#> 2: 
#> 3: reprex::reprex_info()\
#>   

Created by the reprex package v0.1.1.9000 on 2017-11-08
Explanatory variable was numerical, it has been converted
to categorical. In order to avoid this warning, first convert
your explanatory variable to a categorical variable using the
as.factor() function
Show Traceback
Error: Explanatory variable has only one level, it should have at least two levels


#2

Can you provide a sample of your data and what code you have specifically tried to convert your weeks variable from numeric to factor? Also, what package is the inference function from?


#3

Hello,

Thank you Bradley.
I am afraid I do not know what package the function comes from (Google and I are not getting along today) I have loaded
library(statsr)
library(dplyr)
library(ggplot2).

I am new to R so I experimented a bit, my code might not make sense.

nc %>% 
  weeks_new <- as.factor(weeks) %>% 
  inference (y = weeks_new, data=nc, statistic = "mean", type = "ci", method = "theoretical", conf_level = 0,99)

inference (y = weeks_new <- as.factor(weeks), data=nc, statistic = "mean", type = "ci", method = "theoretical", conf_level = 0,99)

nc$weeks_new <- as.factor(nc$weeks)
  inference (y = nc$weeks_new, data=nc, statistic = "mean", type = "ci", method = "theoretical", conf_level = 0,99)

nc <- nc %>%
  mutate(weeks_new = as.factor(weeks)) %>%
  inference (y = weeks_new, data=nc, statistic = "mean", type = "ci", method = "theoretical", conf_level = 0,99)

Do you know a better function for what I need?

The function works when I do not include conf_level = 0,99 in the end. By default I get a confidence level of 95% but need 99%)

inference (y = weeks, data=nc, statistic = "mean", type = "ci", method = "theoretical", conf_level = 0,99)


#4

is the statsr package the package that goes with the coursera course from this github repo? It may be that the inference function is from there. As far as converting weeks to a factor, the third option would be the one that I would do.

As for the 99% CI instead of 95%, it looks like you have conf_level = 0,99. That comma between the 0 and 99 is likely causing the issue. Try changing that to conf_level = 0.99


#5

Hello,

youppiieee!!
It works!! It is odd, I had run a previous code using conf_level = 0,8 with comma and not dot. in the same function so It is surprising it did not work.

Yes its a package from coursera and looks very likely to be from that github repo.

inference (y = weeks, data= nc, statistic = "mean", type= "ci", method="theoretical", conf_level = 0.99)


#6

:tada::dancing_women::zap::balloon:
Just some additional emoji celebration re. your first reprex!! :stuck_out_tongue_winking_eye:


#7

Hahah thanks for the support!! :smile:
I did not dare to post anything again before I had figured it out…

Turns out it was super easy when I watched your video again, first I thought I needed code to copy to the “clipboard” or that it was an actual space in R…:slight_smile:


#8

That’s a great one! It’s all @jennybryan :sparkles: magic! I’m so glad you watched it again, and things clicked! Success! :postal_horn: