我正在使用“鉆石”資料庫我想創建一個函式,該函式接受資料庫中的兩個變數并根據變數(具有多個級別的數字/分類)執行合適的統計檢驗(即卡方、方差分析、t 檢驗)或二分法等。)我的代碼
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))
}
}
它不作業!不列印輸出!
uj5u.com熱心網友回復:
您必須實際呼叫該函式。您目前只定義函式:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/506842.html