簡體   English   中英

Function 為不同變量創建合適的統計測試

[英]Function that creates suitable statistical tests for different variables

我正在使用“鑽石”數據庫我想創建一個 function,它接受來自數據庫的兩個變量並根據變量(具有多個級別的數字/分類)執行合適的統計檢驗(即卡方、方差分析、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))
  }
}
  

它不工作! 不打印輸出!

您必須實際調用 function。 您目前只定義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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM