diff options
Diffstat (limited to 'admin/survey/R')
-rw-r--r-- | admin/survey/R/TempData/.gitignore | 2 | ||||
-rw-r--r-- | admin/survey/R/class.SurveyAnalysisR.php | 346 | ||||
-rw-r--r-- | admin/survey/R/script/createCrosstabulation2.R | 110 | ||||
-rw-r--r-- | admin/survey/R/script/createCrosstabulation3.R | 108 | ||||
-rw-r--r-- | admin/survey/R/script/createCrosstabulation4.R | 108 | ||||
-rw-r--r-- | admin/survey/R/script/createTTest.R | 115 |
6 files changed, 789 insertions, 0 deletions
diff --git a/admin/survey/R/TempData/.gitignore b/admin/survey/R/TempData/.gitignore new file mode 100644 index 0000000..a3a0c8b --- /dev/null +++ b/admin/survey/R/TempData/.gitignore @@ -0,0 +1,2 @@ +*
+!.gitignore
\ No newline at end of file diff --git a/admin/survey/R/class.SurveyAnalysisR.php b/admin/survey/R/class.SurveyAnalysisR.php new file mode 100644 index 0000000..8a594af --- /dev/null +++ b/admin/survey/R/class.SurveyAnalysisR.php @@ -0,0 +1,346 @@ +<?php
+
+/**
+ * @date 11.6.2013
+ *
+ * @author: Peter Hrvatin
+ *
+ * @desc: racunanje analiz z R-jem (klicanje skript, obdelava rezultatov)
+ *
+ */
+
+define("DATA_FOLDER", "admin/survey/R/TempData");
+define("SCRIPT_FOLDER", "admin/survey/R/script");
+
+
+class SurveyAnalysisR {
+
+ public $ank_id; # id ankete
+
+
+ // Konstruktor
+ public function __construct($anketa = null) {
+ global $global_user_id, $site_path, $lang;
+
+ if((int)$anketa > 0){
+
+ $this->ank_id = $anketa;
+ }
+ else {
+ die("Napaka!");
+ }
+ }
+
+
+ // Pozenemo skripto za multicrosstabulacije
+ public function createMultiCrosstabulation($crosstabVars=array(), $avgVar=0, $delezVars=array()){
+ global $site_path;
+ global $lang;
+
+ // Skripta ki jo klicemo (glede na stevilo spremenljivk)
+ $script = $site_path . SCRIPT_FOLDER . '/';
+ $script .= 'createCrosstabulation'.count($crosstabVars).'.R';
+
+
+ // Prvi parameter je pot do r folderja
+ $params = $site_path;
+
+
+ // Ce racunamo povprecje (avgVar==1)
+ $params .= ' '.$avgVar;
+
+
+ // variable pri katerih upostevamo delez
+ $delezVar = 0;
+ if(count($delezVars) > 0){
+
+ // Gre za checkbox
+ if($delezVars == -1){
+ $params .= ' -1';
+ }
+ else{
+ $params .= ' ';
+ $cnt = 1;
+ foreach($delezVars as $var){
+ if($var == '1')
+ $params .= $cnt.',';
+
+ $cnt++;
+ }
+ $params = substr($params, 0, -1);
+ }
+
+ $delezVar = 1;
+ }
+
+
+ // pozenemo skripto
+ $out = shell_exec('Rscript '.$script.' '.$params);
+
+
+ //Rscript c:/Programs/WAMP/www/FDV/admin/survey/R/script/createCrosstabulation2.R c:/Programs/WAMP/www/FDV/ 0 -1
+
+
+ // Napolnimo rezultate v array
+ $results = $this->parseMultiCrosstabResults($out, $avgVar, $delezVar);
+
+ return $results;
+ }
+
+ // Napolnimo podatke iz vrnjenega rezultata
+ function parseMultiCrosstabResults($out, $avgVar=0, $delezVar=0){
+
+ $results = array();
+
+ // Razbijemo na variable, podatke in vsote
+ $strings = explode("--", $out);
+
+
+ // VARIABLE shranimo v array
+ $varsString = explode("_", $strings[0]);
+ $vars = array();
+ foreach($varsString as $varString){
+ $vars[] = explode(",", $varString);
+ }
+ //$results['vars'] = $vars;
+
+
+ // PODATKE shranimo v array
+ $tempData = explode("_", $strings[1]);
+ $tempAvgData = explode("_", $strings[6]);
+ $tempDelezData = ($avgVar == 1) ? explode("_", $strings[7]) : explode("_", $strings[6]);
+ $data = array();
+ $avgData = array();
+ $delezData = array();
+ $varCount = count($vars);
+
+ // crosstab na 2 variablah
+ if($varCount == 2){
+
+ $dataKey = 0;
+ foreach($vars[1] as $key2 => $var2){
+ foreach($vars[0] as $key1 => $var1){
+
+ if((int)$var1 != -1 && (int)$var2 != -1 && $tempData[$dataKey] != null && $tempData[$dataKey] != 0)
+ $data[(int)$var1][(int)$var2] = (int)$tempData[$dataKey];
+
+ // Povprecje ce ga imamo
+ if($avgVar == 1 && (int)$var1 != -1 && (int)$var2 != -1 && $tempAvgData[$dataKey] != null && $tempAvgData[$dataKey] != 0)
+ $avgData[(int)$var1][(int)$var2] = (float)$tempAvgData[$dataKey];
+
+ // Delez ce ga imamo
+ if($delezVar == 1 && (int)$var1 != -1 && (int)$var2 != -1 && $tempDelezData[$dataKey] != null && $tempDelezData[$dataKey] != 0)
+ $delezData[(int)$var1][(int)$var2] = (float)$tempDelezData[$dataKey];
+
+ $dataKey++;
+ }
+ }
+ $results['crosstab'] = $data;
+ $results['avg'] = $avgData;
+ $results['delez'] = $delezData;
+
+ // VSOTE shranimo v array
+ $sumsTemp = array_map('intval', explode("_", $strings[2]));
+ $sums = array();
+ foreach($sumsTemp as $sumKey => $sum){
+ $key = (int)$vars[0][$sumKey];
+ $sums[$key] = $sum;
+ }
+ $results['sumaVrstica'] = $sums;
+
+ $sumsTemp = array_map('intval', explode("_", $strings[3]));
+ $sums = array();
+ foreach($sumsTemp as $sumKey => $sum){
+ $key = (int)$vars[1][$sumKey];
+ $sums[$key] = $sum;
+ }
+ $results['sumaStolpec'] = $sums;
+
+ $sums = $strings[4];
+ $results['sumaSkupna'] = (int)$sums;
+ }
+ // crosstab na 3 variablah
+ elseif($varCount == 3){
+
+ $dataKey = 0;
+ foreach($vars[2] as $key3 => $var3){
+ foreach($vars[1] as $key2 => $var2){
+ foreach($vars[0] as $key1 => $var1){
+
+ if((int)$var1 != -1 && (int)$var2 != -1 && (int)$var3 != -1 && $tempData[$dataKey] != null && $tempData[$dataKey] != 0)
+ $data[(int)$var1][(int)$var2][(int)$var3] = (int)$tempData[$dataKey];
+
+ // Povprecje ce ga imamo
+ if($avgVar == 1 && (int)$var1 != -1 && (int)$var2 != -1 && (int)$var3 != -1 && $tempAvgData[$dataKey] != null && $tempAvgData[$dataKey] != 0)
+ $avgData[(int)$var1][(int)$var2][(int)$var3] = (float)$tempAvgData[$dataKey];
+
+ // Delez ce ga imamo
+ if($delezVar == 1 && (int)$var1 != -1 && (int)$var2 != -1 && (int)$var3 != -1 && $tempDelezData[$dataKey] != null && $tempDelezData[$dataKey] != 0)
+ $delezData[(int)$var1][(int)$var2][(int)$var3] = (float)$tempDelezData[$dataKey];
+
+ $dataKey++;
+ }
+ }
+ }
+ $results['crosstab'] = $data;
+ $results['avg'] = $avgData;
+ $results['delez'] = $delezData;
+
+ // VSOTE shranimo v array
+ $sumsTemp = array_map('intval', explode("_", $strings[2]));
+ $sums = array();
+ foreach($sumsTemp as $sumKey => $sum){
+ $key = (int)$vars[0][$sumKey];
+ $sums[$key] = $sum;
+ }
+ $results['sumaVrstica'] = $sums;
+
+ $sumsTemp = array_map('intval', explode("_", $strings[3]));
+ $sums = array();
+ foreach($sumsTemp as $sumKey => $sum){
+ $key = (int)$vars[1][$sumKey];
+ $sums[$key] = $sum;
+ }
+ $results['sumaStolpec'] = $sums;
+
+ $sums = $strings[4];
+ $results['sumaSkupna'] = (int)$sums;
+ }
+ // crosstab na 4 variablah
+ elseif($varCount == 4){
+
+ $dataKey = 0;
+ foreach($vars[3] as $key4 => $var4){
+ foreach($vars[2] as $key3 => $var3){
+ foreach($vars[1] as $key2 => $var2){
+ foreach($vars[0] as $key1 => $var1){
+
+ if((int)$var1 != -1 && (int)$var2 != -1 && (int)$var3 != -1 && (int)$var4 != -1 && $tempData[$dataKey] != null && $tempData[$dataKey] != 0)
+ $data[(int)$var1][(int)$var2][(int)$var3][(int)$var4] = (int)$tempData[$dataKey];
+
+ // Povprecje ce ga imamo
+ if($avgVars == 1 && (int)$var1 != -1 && (int)$var2 != -1 && (int)$var3 != -1 && (int)$var4 != -1 && $tempAvgData[$dataKey] != null && $tempAvgData[$dataKey] != 0)
+ $avgData[(int)$var1][(int)$var2][(int)$var3][(int)$var4] = (float)$tempAvgData[$dataKey];
+
+ // Delez ce ga imamo
+ if($delezVars == 1 && (int)$var1 != -1 && (int)$var2 != -1 && (int)$var3 != -1 && (int)$var4 != -1 && $tempDelezData[$dataKey] != null && $tempDelezData[$dataKey] != 0)
+ $delezData[(int)$var1][(int)$var2][(int)$var3][(int)$var4] = (float)$tempDelezData[$dataKey];
+
+ $dataKey++;
+ }
+ }
+ }
+ }
+ $results['crosstab'] = $data;
+ $results['avg'] = $avgData;
+ $results['delez'] = $delezData;
+
+ // VSOTE shranimo v array
+ $sumsTemp = array_map('intval', explode("_", $strings[2]));
+ $sums = array();
+ foreach($sumsTemp as $sumKey => $sum){
+ $key = (int)$vars[0][$sumKey];
+ $sums[$key] = $sum;
+ }
+ $results['sumaVrstica'] = $sums;
+
+ $sumsTemp = array_map('intval', explode("_", $strings[3]));
+ $sums = array();
+ foreach($sumsTemp as $sumKey => $sum){
+ $key = (int)$vars[1][$sumKey];
+ $sums[$key] = $sum;
+ }
+ $results['sumaStolpec'] = $sums;
+
+ $sums = $strings[4];
+ $results['sumaSkupna'] = (int)$sums;
+ }
+ else{
+ $data = null;
+ }
+
+
+ // X^2
+ $x2 = $strings[5];
+ $results['hi2'] = (float)$x2;
+
+
+ return $results;
+ }
+
+
+ // Pozenemo skripto za ttest
+ public function createTTest($vals){
+ global $site_path;
+ global $lang;
+
+ // Skripta ki jo klicemo (glede na stevilo spremenljivk)
+ $script = $site_path . SCRIPT_FOLDER . '/';
+ $script .= 'createTTest.R';
+
+
+ // Prvi parameter je pot do r folderja
+ $params = $site_path;
+
+
+ // Drugi in tretji parameter - vrednost ki jo upostevamo (ce je checkbox sta oba 1 in jo kasneje ignoriramo)
+ $params .= ' '.$vals[0].' '.$vals[1];
+
+
+ // pozenemo skripto
+ $out = shell_exec('Rscript '.$script.' '.$params);
+ //echo $out;
+
+ // Napolnimo rezultate v array
+ $results = $this->parseTTestResults($out);
+
+ //Rscript c:/Programs/WAMP/www/FDV/admin/survey/R/script/createTTest.R c:/Programs/WAMP/www/FDV/
+
+ return $results;
+ }
+
+ // Napolnimo podatke iz vrnjenega rezultata
+ function parseTTestResults($out){
+
+ $results = array();
+
+
+ // Razbijemo na variable, podatke in vsote
+ $strings = explode("--", $out);
+
+ // Vrednosti prve variable
+ $vals1 = explode("_", $strings[0]);
+ $results['1'] = array(
+ 'n' => (int)$vals1[0],
+ 'x' => (float)$vals1[1],
+ 's2' => (float)$vals1[2],
+ 'se' => (float)$vals1[3],
+ 'se2' => (float)$vals1[4],
+ 'margin' => (float)$vals1[5]
+ );
+
+ // Vrednosti druge variable
+ $vals2 = explode("_", $strings[1]);
+ $results['2'] = array(
+ 'n' => (int)$vals2[0],
+ 'x' => (float)$vals2[1],
+ 's2' => (float)$vals2[2],
+ 'se' => (float)$vals2[3],
+ 'se2' => (float)$vals2[4],
+ 'margin' => (float)$vals2[5]
+ );
+
+ // Skupne vrednosti
+ $vals = explode("_", $strings[2]);
+ $results['d'] = (float)$vals[0];
+ $results['sed'] = (float)$vals[1];
+ $results['t'] = (float)$vals[2];
+ $results['sig'] = (float)$vals[3];
+
+
+ return $results;
+ }
+
+}
+
+?>
\ No newline at end of file diff --git a/admin/survey/R/script/createCrosstabulation2.R b/admin/survey/R/script/createCrosstabulation2.R new file mode 100644 index 0000000..c8562e5 --- /dev/null +++ b/admin/survey/R/script/createCrosstabulation2.R @@ -0,0 +1,110 @@ +# CROSSTABULACIJA 2 SPREMENLJIVK
+
+# preberemo parametre 1->folderpath
+params <- commandArgs(trailingOnly=TRUE)
+
+path <- params[1]
+
+# preberemo sfiltrirano tabelo s podatki
+dataTable <- read.table(
+ file = paste(path, 'admin/survey/R/TempData/crosstab_data.tmp', sep=''),
+ sep = ',',
+ colClasses = 'numeric',
+ comment.char = '',
+ quote = '',
+ header = FALSE
+)
+
+
+# KROSTABULACIJA
+# iz tabele pobrisemo vrstice z vrednostmi manjsimi od 0 in sfiltriramo vse stolpce ki jih ne rabimo
+dataTableClean <- dataTable[apply(dataTable[,1:2], MARGIN = 1, function(x) all(x > -1)), ]
+
+# izvedemo krostabulacijo
+crosstabTable <- xtabs(~dataTableClean[,1]+dataTableClean[,2], data=dataTableClean)
+
+# Vrednosti katerim pripadajo frekvence
+vars <- dimnames(crosstabTable)
+
+# izracunamo vsoto
+sumsVrstica <- apply(crosstabTable, c(1), sum)
+sumsStolpec <- apply(crosstabTable, c(2), sum)
+sums <- sum(crosstabTable)
+
+# x^2
+x2 <- summary(crosstabTable);
+x2 <- x2[3]
+
+
+delezCol <- 3
+
+# POVPRECJE
+if(params[2] == '1'){
+
+ #dataTableAvg <- dataTableClean[sapply(dataTableClean[,3], function(x) all(x > -1)), ]
+ dataTableAvg <- dataTableClean
+ dataTableAvg[,3][dataTableAvg[,3] < 0] <- 0
+
+ avgTable <- xtabs(dataTableAvg[,3]~dataTableAvg[,1]+dataTableAvg[,2], dataTableAvg) / crosstabTable
+
+ delezCol <- delezCol + 1
+}
+
+
+# DELEZ
+if(length(params) > 2){
+
+ # array, ki vsebuje vrednosti stolpca, za katere racunamo delez
+ delez <- params[3]
+ delez <- unlist(strsplit(delez, ","))
+ delez <- sapply(delez, strtoi)
+
+ # ce je -1 gre za checxbox stolpce ki imajo samo vrednost 1 in jih je vec
+ if(delez[1] == -1){
+ dataTableDelez <- dataTableClean
+ dataTableDelez[,delezCol][!(rowSums(dataTableDelez[delezCol:ncol(dataTableDelez)]) == ncol(dataTableDelez)-delezCol+1)] <- 0
+ dataTableDelez[,delezCol][(rowSums(dataTableDelez[delezCol:ncol(dataTableDelez)]) == ncol(dataTableDelez)-delezCol+1)] <- 1
+ }
+ else{
+ #dataTableDelez <- dataTableClean[sapply(dataTableClean[,delezCol], function(x) all(x %in% delez)), ]
+ dataTableDelez <- dataTableClean
+ dataTableDelez[,delezCol][!(dataTableDelez[,delezCol] %in% delez)] <- 0
+ dataTableDelez[,delezCol][(dataTableDelez[,delezCol] %in% delez)] <- 1
+ }
+
+ delezTable <- xtabs(dataTableDelez[,delezCol]~dataTableDelez[,1]+dataTableDelez[,2], dataTableDelez) / crosstabTable
+}
+
+
+# vsako variablo (vrstico) pretvorimo v csv string
+vars <- sapply(vars, paste, collapse=",")
+
+# naredimo 2-d tabelo podatkov
+crosstabTable <- ftable(crosstabTable)
+crosstabTable <- sapply(crosstabTable, paste, collapse=", ")
+
+
+# podatke vrnemo v obliki var11,var12,var13..._var2...--freq1_freq2...
+cat(paste(vars, sep="", collapse="_"))
+cat("--")
+cat(paste(crosstabTable, sep="", collapse="_"))
+cat("--")
+cat(paste(sumsVrstica, sep="", collapse="_"))
+cat("--")
+cat(paste(sumsStolpec, sep="", collapse="_"))
+cat("--")
+cat(paste(sums, sep="", collapse="_"))
+cat("--")
+cat(paste(x2, sep="", collapse="_"))
+
+# rezultati povprecja
+if(params[2] == '1'){
+ cat("--")
+ cat(paste(avgTable, sep="", collapse="_"))
+}
+
+# rezultati deleza
+if(length(params) > 2){
+ cat("--")
+ cat(paste(delezTable, sep="", collapse="_"))
+}
\ No newline at end of file diff --git a/admin/survey/R/script/createCrosstabulation3.R b/admin/survey/R/script/createCrosstabulation3.R new file mode 100644 index 0000000..13985ae --- /dev/null +++ b/admin/survey/R/script/createCrosstabulation3.R @@ -0,0 +1,108 @@ +# CROSSTABULACIJA 3 SPREMENLJIVK
+
+# preberemo parametre 1->folderpath
+params <- commandArgs(trailingOnly=TRUE)
+
+path <- params[1]
+
+# preberemo sfiltrirano tabelo s podatki
+dataTable <- read.table(
+ file = paste(path, 'admin/survey/R/TempData/crosstab_data.tmp', sep=''),
+ sep = ',',
+ colClasses = 'numeric',
+ comment.char = '',
+ quote = '',
+ header = FALSE
+)
+
+
+# iz tabele pobrisemo vrstice z vrednostmi manjsimi od 0 in sfiltriramo vse stolpce ki jih ne rabimo
+dataTableClean <- dataTable[apply(dataTable[,1:3], MARGIN = 1, function(x) all(x > -1)), 1:3]
+
+
+
+# izvedemo krostabulacijo
+crosstabTable <- xtabs(~dataTableClean[,1]+dataTableClean[,2]+dataTableClean[,3], data=dataTableClean)
+
+
+
+# Vrednosti katerim pripadajo frekvence
+vars <- dimnames(crosstabTable)
+
+# izracunamo vsoto
+sumsVrstica <- apply(crosstabTable, c(2), sum)
+sumsStolpec <- apply(crosstabTable, c(3), sum)
+sums <- sum(crosstabTable)
+
+
+delezCol <- 4
+
+# POVPRECJE
+if(params[2] == '1'){
+
+ #dataTableAvg <- dataTableClean[sapply(dataTableClean[,4], function(x) all(x > -1)), ]
+ dataTableAvg <- dataTableClean
+ dataTableAvg[,4][dataTableAvg[,4] < 0] <- 0
+
+ avgTable <- xtabs(dataTableAvg[,4]~dataTableAvg[,1]+dataTableAvg[,2]+dataTableAvg[,3], dataTableAvg) / crosstabTable
+
+ delezCol <- delezCol + 1
+}
+
+
+# DELEZ
+if(length(params) > 2){
+
+ # array, ki vsebuje vrednosti stolpca, za katere racunamo delez
+ delez <- params[3]
+ delez <- unlist(strsplit(delez, ","))
+ delez <- sapply(delez, strtoi)
+
+ # ce je -1 gre za checxbox stolpce ki imajo samo vrednost 1 in jih je vec
+ if(delez[1] == -1){
+ dataTableDelez <- dataTableClean
+ dataTableDelez[,delezCol][!(rowSums(dataTableDelez[delezCol:ncol(dataTableDelez)]) == ncol(dataTableDelez)-delezCol+1)] <- 0
+ dataTableDelez[,delezCol][(rowSums(dataTableDelez[delezCol:ncol(dataTableDelez)]) == ncol(dataTableDelez)-delezCol+1)] <- 1
+ }
+ else{
+ #dataTableDelez <- dataTableClean[sapply(dataTableClean[,delezCol], function(x) all(x %in% delez)), ]
+ dataTableDelez[,delezCol][!(dataTableDelez[,delezCol] %in% delez)] <- 0
+ dataTableDelez[,delezCol][(dataTableDelez[,delezCol] %in% delez)] <- 1
+ }
+
+ delezTable <- xtabs(dataTableDelez[,delezCol]~dataTableDelez[,1]+dataTableDelez[,2]+dataTableDelez[,3], dataTableDelez) / crosstabTable
+}
+
+
+# vsako variablo (vrstico) pretvorimo v csv string
+vars <- sapply(vars, paste, collapse=",")
+
+# naredimo 2-d tabelo podatkov
+crosstabTable <- ftable(crosstabTable)
+crosstabTable <- sapply(crosstabTable, paste, collapse=", ")
+
+
+# podatke vrnemo v obliki var11,var12,var13..._var2...DATAfreq1_freq2...
+cat(paste(vars, sep="", collapse="_"))
+cat("--")
+cat(paste(crosstabTable, sep="", collapse="_"))
+cat("--")
+cat(paste(sumsVrstica, sep="", collapse="_"))
+cat("--")
+cat(paste(sumsStolpec, sep="", collapse="_"))
+cat("--")
+cat(paste(sums, sep="", collapse="_"))
+cat("--")
+cat(paste(x2, sep="", collapse="_"))
+
+# rezultati povprecja
+if(params[2] == '1'){
+ cat("--")
+ cat(paste(avgTable, sep="", collapse="_"))
+}
+
+# rezultati deleza
+if(length(params) > 2){
+ cat("--")
+ cat(paste(delezTable, sep="", collapse="_"))
+}
\ No newline at end of file diff --git a/admin/survey/R/script/createCrosstabulation4.R b/admin/survey/R/script/createCrosstabulation4.R new file mode 100644 index 0000000..6dd4784 --- /dev/null +++ b/admin/survey/R/script/createCrosstabulation4.R @@ -0,0 +1,108 @@ +# CROSSTABULACIJA 3 SPREMENLJIVK
+
+# preberemo parametre 1->folderpath
+params <- commandArgs(trailingOnly=TRUE)
+
+path <- params[1]
+
+# preberemo sfiltrirano tabelo s podatki
+dataTable <- read.table(
+ file = paste(path, 'admin/survey/R/TempData/crosstab_data.tmp', sep=''),
+ sep = ',',
+ colClasses = 'numeric',
+ comment.char = '',
+ quote = '',
+ header = FALSE
+)
+
+
+# iz tabele pobrisemo vrstice z vrednostmi manjsimi od 0 in sfiltriramo vse stolpce ki jih ne rabimo
+dataTableClean <- dataTable[apply(dataTable[,1:4], MARGIN = 1, function(x) all(x > -1)), 1:4]
+
+
+
+# izvedemo krostabulacijo
+crosstabTable <- xtabs(~dataTableClean[,1]+dataTableClean[,2]+dataTableClean[,3]+dataTableClean[,4], data=dataTableClean)
+
+
+
+# Vrednosti katerim pripadajo frekvence
+vars <- dimnames(crosstabTable)
+
+# izracunamo vsoto
+sumsVrstica <- apply(crosstabTable, c(3), sum)
+sumsStolpec <- apply(crosstabTable, c(4), sum)
+sums <- sum(crosstabTable)
+
+
+delezCol <- 5
+
+# POVPRECJE
+if(params[2] == '1'){
+
+ #dataTableAvg <- dataTableClean[sapply(dataTableClean[,5], function(x) all(x > -1)), ]
+ dataTableAvg <- dataTableClean
+ dataTableAvg[,5][dataTableAvg[,5] < 0] <- 0
+
+ avgTable <- xtabs(dataTableAvg[,5]~dataTableAvg[,1]+dataTableAvg[,2]+dataTableAvg[,3]+dataTableAvg[,4], dataTableAvg) / crosstabTable
+
+ delezCol <- delezCol + 1
+}
+
+
+# DELEZ
+if(length(params) > 2){
+
+ # array, ki vsebuje vrednosti stolpca, za katere racunamo delez
+ delez <- params[3]
+ delez <- unlist(strsplit(delez, ","))
+ delez <- sapply(delez, strtoi)
+
+ # ce je -1 gre za checxbox stolpce ki imajo samo vrednost 1 in jih je vec
+ if(delez[1] == -1){
+ dataTableDelez <- dataTableClean
+ dataTableDelez[,delezCol][!(rowSums(dataTableDelez[delezCol:ncol(dataTableDelez)]) == ncol(dataTableDelez)-delezCol+1)] <- 0
+ dataTableDelez[,delezCol][(rowSums(dataTableDelez[delezCol:ncol(dataTableDelez)]) == ncol(dataTableDelez)-delezCol+1)] <- 1
+ }
+ else{
+ #dataTableDelez <- dataTableClean[sapply(dataTableClean[,delezCol], function(x) all(x %in% delez)), ]
+ dataTableDelez[,delezCol][!(dataTableDelez[,delezCol] %in% delez)] <- 0
+ dataTableDelez[,delezCol][(dataTableDelez[,delezCol] %in% delez)] <- 1
+ }
+
+ delezTable <- xtabs(dataTableDelez[,delezCol]~dataTableDelez[,1]+dataTableDelez[,2]+dataTableDelez[,3]+dataTableDelez[,4], dataTableDelez) / crosstabTable
+}
+
+
+# vsako variablo (vrstico) pretvorimo v csv string
+vars <- sapply(vars, paste, collapse=",")
+
+# naredimo 2-d tabelo podatkov
+crosstabTable <- ftable(crosstabTable)
+crosstabTable <- sapply(crosstabTable, paste, collapse=", ")
+
+
+# podatke vrnemo v obliki var11,var12,var13..._var2...DATAfreq1_freq2...
+cat(paste(vars, sep="", collapse="_"))
+cat("--")
+cat(paste(crosstabTable, sep="", collapse="_"))
+cat("--")
+cat(paste(sumsVrstica, sep="", collapse="_"))
+cat("--")
+cat(paste(sumsStolpec, sep="", collapse="_"))
+cat("--")
+cat(paste(sums, sep="", collapse="_"))
+cat("--")
+cat(paste(x2, sep="", collapse="_"))
+
+# rezultati povprecja
+if(params[2] == '1'){
+ cat("--")
+ cat(paste(avgTable, sep="", collapse="_"))
+}
+
+# rezultati deleza
+if(length(params) > 2){
+ cat("--")
+ cat(paste(delezTable, sep="", collapse="_"))
+}
\ No newline at end of file diff --git a/admin/survey/R/script/createTTest.R b/admin/survey/R/script/createTTest.R new file mode 100644 index 0000000..e3cd83f --- /dev/null +++ b/admin/survey/R/script/createTTest.R @@ -0,0 +1,115 @@ +# preberemo parametre 1->folderpath
+params <- commandArgs(trailingOnly=TRUE)
+
+path <- params[1]
+
+# preberemo sfiltrirano tabelo s podatki
+dataTable <- read.table(
+ file = paste(path, 'admin/survey/R/TempData/ttest_data.tmp', sep=''),
+ sep = ',',
+ colClasses = 'numeric',
+ comment.char = '',
+ quote = '',
+ header = FALSE
+)
+
+
+
+# ce ni checkbox moramo podatke preurediti (imamo 2 dodatna parametra)
+if(params[3] > 1){
+
+ val1 <- params[2]
+ val2 <- params[3]
+
+ dataTableClean <- dataTable[sapply(dataTable[,1], function(x) all((x == val1) || (x == val2))), ]
+
+ dataTable[,1][dataTable[,1] != val1] <- 0
+ dataTable[,1][dataTable[,1] == val1] <- 1
+
+ dataTable[,2][dataTable[,2] != val2] <- 0
+ dataTable[,2][dataTable[,2] == val2] <- 1
+}
+
+
+# pocistimo vse vrstice, ki imajo za numeric (col 3) missing (< 0)
+dataTable <- dataTable[sapply(dataTable[,3], function(x) all(x > -1)), ]
+dataTableClean <- dataTableClean[sapply(dataTableClean[,3], function(x) all(x > -1)), ]
+
+
+# podmnozica vrednosti glede na 1. vrednost
+col1 <- subset(dataTable, dataTable[,1] == 1)
+# podmnozica vrednosti glede na 2. vrednost
+col2 <- subset(dataTable, dataTable[,2] == 1)
+
+
+# n
+n1 <- nrow(col1)
+n2 <- nrow(col2)
+
+# avg(x)
+avg1 <- mean(col1[,3])
+if(is.nan(avg1))
+ avg1 <- 0
+avg2 <- mean(col2[,3])
+if(is.nan(avg2))
+ avg2 <- 0
+
+# standardna deviacija (s^2??)
+sd1 <- sd(col1[,3])
+if(is.na(sd1))
+ sd1 <- 0
+sd2 <- sd(col2[,3])
+if(is.na(sd2))
+ sd2 <- 0
+
+# standardna napaka
+se1 = sd1 / sqrt(n1)
+if(is.nan(se1))
+ se1 <- 0
+se2 = sd2 / sqrt(n2)
+if(is.nan(se2))
+ se2 <- 0
+
+# kvadrat standardne napake
+se21 <- se1^2
+se22 <- se2^2
+
+# margini (1,96 * se)
+mar1 <- 1.96 * se1
+mar2 <- 1.96 * se2
+
+
+
+# izvedemo ttest (ce obstajata po vsaj dva primera vsake binarni vrednosti)
+if(n1 > 1 && n2 > 1){
+ ttest <- t.test(dataTableClean[,3]~dataTableClean[,1], var.equal=FALSE)
+
+ # razlika povprečij => $d = x1 -x2
+ d <- avg1 - avg2
+
+ # sed (std. error difference)
+ #sed <- sqrt(se21 + se22)
+ #sed <- ttest$estimate/ttest$statistic
+ sed <- (-1 * diff(ttest$estimate) / ttest$statistic)
+
+ #T <- d / sed
+ T <- ttest$statistic
+
+ # signifikanca
+ sig <- ttest$p.value
+
+} else{
+ d <- 0
+ sed <- 0
+ T <- 0
+ sig <- 0
+}
+
+
+
+# podatke vrnemo v x1_x2_x3...--y1_y2...
+cat(paste(n1, avg1, sd1, se1, se21, mar1, sep="_"))
+cat("--")
+cat(paste(n2, avg2, sd2, se2, se22, mar2, sep="_"))
+cat("--")
+cat(paste(d, sed, T, sig, sep="_"))
\ No newline at end of file |