[R] Need help with function writing
Wayne.W.Jones at shell.com
Wayne.W.Jones at shell.com
Thu Sep 27 10:09:26 CEST 2007
Why dont you use the t.test within R? See help(t.test). It looks to have everything you need:
here are the examples with different alternative hypothesese:
with(sleep, t.test(extra[group == 1], extra[group == 2],alternative = "greater"))
with(sleep, t.test(extra[group == 1], extra[group == 2],alternative = "two.sided"))
with(sleep, t.test(extra[group == 1], extra[group == 2],alternative = "less"))
If you are still really keen to use your own function I would use help(switch) to select between different alternative hypths.
Regards
Wayne
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org]On Behalf Of Letticia Ramlal
Sent: 25 September 2007 14:20
To: r-help at stat.math.ethz.ch
Subject: [R] Need help with function writing
Hello:
If anyone could guide me with this I would greatly appreciate it. Thanking you in advance for your assistance.
Using a 3-level input factor alternative so that a function(below) can compute both a two-sided and one-sided p-values. Making the two-sided test the default. And produce output information about which alternative was tested. Where would I place the ifelse statement?
function(yvec,trtvec,alpha=0.05,header="") {
#################################################
# A function to compute a two-sample t-test and confidence
# interval (equal-variance, independent samples). yvec is
# a numeric vector containing both samples' data. trtvec
# is a vector, same length as yvec, of treatment
# identifiers for the data in yvec. A boxplot comparing
# the treatments' data is constructed. Output is a one-row
# data frame reporting the results of the test and
# confidence interval
##################################################
trtvec=as.factor(trtvec)
boxplot(split(yvec,trtvec))
title(header)
ybar=tapply(yvec,trtvec,mean)
varvec=tapply(yvec,trtvec,var)
nvec=table(trtvec)
error.df=nvec[1]+nvec[2]-2
pooled.var=((nvec[1]-1)*varvec[1]+(nvec[2]-1)*varvec[2])/error.df
diff12estimate=ybar[1]-ybar[2]
stderr=sqrt(pooled.var*((1/nvec[1])+(1/nvec[2])))
tratio=diff12estimate/stderr
twosidedP=2*(1-pt(abs(tratio),error.df))
tcrit=qt(1-alpha/2,error.df)
lower=diff12estimate-tcrit*stderr
upper=diff12estimate+tcrit*stderr
calpha=1-alpha
out=data.frame(diff12estimate,stderr,tratio,twosidedP,lower,upper,alpha)
names(out)=c("Estimator","SE","T","P-value","Lower CI","Upper CI","Confidence")
out
}
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list