I am using "diamonds" database I want to create a function that accepts two variables from the database and perform a suitable statistical test (i.e., Chi-square, ANOVA, t-test) according to the variables (numeric/ categorical with multiple levels or dichotomous, etc..) My code
DB= diamonds
dependent_var= DB$carat
independent_var=DB$depth
my_function = function(dependent_var,independent_var){
if(is.numeric(dependent_var) & is.numeric(independent_var)){
c=cor.test(independent_var,dependent_var)
print(paste("A person correlation is done between dependent and independent variable the correlation coeffeicent is",
round(c$estimate,3),"and the pv is",c$p.value))
}
if(is.factor(dependent_var)&is.factor(independent_var)){
q=chisq.test(independent_var,dependent_var)
print(paste("A chi-square is done between dependent and independent and the pv is",q$p.value))
}
}
IT IS NOT WORKING! NOT PRINTING THE OUTPUT!
CodePudding user response:
You have to actually call the function. You currently only define the function:
library(ggplot2)
data(diamonds)
dep_var = diamonds$carat
indep_var = diamonds$depth
my_function = function(dependent_var, independent_var){
if(is.numeric(dependent_var) & is.numeric(independent_var)){
c = cor.test(independent_var,dependent_var)
print(paste("The Pearson correlation is calculated for the dependent and independent variable. The correlation coefficient is ",
round(c$estimate, 3)," and the p-value is ", c$p.value))
}
if(is.factor(dependent_var) & is.factor(independent_var)){
q = chisq.test(independent_var,dependent_var)
print(paste("A chi-square test is applied on the dependent and independent variable. The p-value is ", q$p.value))
}
}
# Call of the function. This is missing:
my_function(dependent_var = dep_var, independent_var = indep_var)