首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算方差

计算方差
EN

Stack Overflow用户
提问于 2016-02-24 09:07:23
回答 2查看 349关注 0票数 0

我有自由度和中庸之道。我需要计算标准差。

任何帮助都是非常感谢的。谢谢

EN

回答 2

Stack Overflow用户

发布于 2016-02-24 10:34:01

仍然不是100%确定你要做什么,但这里有一次尝试(使用t统计量的95%范围)……我只有一个文件(这是你粘贴的3个元素),它的名字是stats1。我正在使用awk从文件中提取相关字段,并将其抛出给R。

代码语言:javascript
复制
awk -F'>' '$1 ~/Overall/{print $2}' stats* | Rscript -e 'qt( 0.975, as.numeric (readLines ("stdin"))) '
[1]  2.940089  2.776445 12.706205
票数 0
EN

Stack Overflow用户

发布于 2016-02-24 14:35:54

将一些文本处理或数据争论工具与诸如R之类的统计包结合使用当然是有意义的(特别是,您可能想要使用R的t.test(VECTOR, MU)),但是awk肯定能够胜任这项任务,甚至可能具有某些优势。无论如何,我已经附加了一个(bash)脚本,该脚本期望它的输入每行都有一个数据观察值,并按照脚本的“帮助”文档中的描述发出一行。

下面是使用t.test()从输出中提取的代码和使用脚本(t-test)运行的代码,以供比较:

代码语言:javascript
复制
$ R
> t.test(c(1,2,3), mu=0)
    One Sample t-test
t = 3.4641, df = 2, p-value = 0.07418
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -0.4841377  4.4841377

$ (echo 1; echo 2; echo 3) | t-stat
3 2 0.666667 3.4641
代码语言:javascript
复制
#!/bin/bash
# USE AT YOUR OWN RISK
# For help: $0 --help
VERSION=0.1.1

# Requires: awk

BN=$(basename "$0")

function help {
cat <<EOF
Syntax: $0 [n1 [FILE]]

Compute the sample mean, the sample variance, and the t-statistic for
the array of numbers in column n1 of the input, where n1 defaults to 1,
and FILE defaults to STDIN.

The output consists of a single line of four values:

n mean variance t

where "n" is the sample size (possibly 0),
      "mean" is the sample mean,
      "variance" is the average of the squared deviations from the mean, and
      "t" is the sample t-statistic, computed as 
           mean * sqrt( n * ss / (n-1));

If a statistic cannot be computed, an asterisk (*) is used in its place.

If the value in the specified field in the input is null or
non-numeric, it is ignored.

If a file is specified as "-" then STDIN is read.

Example:

$BN <<< $'1\n2\n3'
3 2 2 3.4641

EOF
}

function die     { echo "$BN: $@" >&2 ; exit 1 ; }

case "$1" in
    -h | --help )
    help
    exit
    ;;
    -v | --verbose )
    VERBOSE=1
    shift
    ;;
esac

# Default:

file=-

if [ $# = 0 ] ; then
    f1=1
elif [ "$1" = - ] ; then
    f1=1
else
   f1="$1"
   shift
   check=`expr "$f1" : "\([0-9]*\)"`
   if [ "$f1" != "$check" ] ; then die a non-numeric field was specified ; fi
fi

if [ $# != 0 ] ; then file="$1" ; fi

awk -v verbose="$VERBOSE" -v f1="$f1" '
  BEGIN { a1=0; s1=0; n=0; }
  NF < f1 { next }
  $f1 == "" || ($f1+0 != $f1) { next }
  { n++; a1 += $f1; s1 += $f1*$f1; }

  END {
    s="*";
    if ( n == 0 ) {print 0, s, s, s; exit }
    avg = a1/n;
    var = s1/n - avg*avg;
    if (var == 0) { print n, avg, var, s; exit}
    tstat = avg * sqrt( (n-1) / var);
    print n, avg, var, tstat; 
  } ' "$file"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35591405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档