[R] hypothesis testing

Leeds, Mark (IED) Mark.Leeds at morganstanley.com
Mon Sep 24 21:55:16 CEST 2007


This was sent to me by someone on the R-list ( I don't know her ) but I
don't have time to look at this right now so I told her I would send
it to the R-list because she said it keeps getting bounced when she
sends it.
  
#=======================================================================
====================================================================

I am a bit confused with this. If possible do you think you can help me
with this? 
 
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
}
 
I need to alter the above function twosamp so that its output includes
the decision made by the hypothesis test. For any alpha level, the
function should say ' Reject Null' or 'Fail to Reject Null' where it is
necessary. 

Use a new 3-level input factor alternative so that the function
twosamp(above) can compute both two-sided and one-sided p-values and
make the two-sided test the default and output information about which
alternative was tested. Therefore ifelse should work here right.

Use the following code 

Var.test(yvec~as.factor(trtvec))$p.value>alpha. 
If the variances are not equal then use
Sqrt((varvec[1]/nvec[1])+(varvec[2]/nvec[2])) Ifelse should be used to
allow for either standard error formula, depending on whether the
equal-variances assumption is valid or not.

 

How can I change the output from a data frame to a list?
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to buy/sell the
securities/instruments mentioned or an official confirmation.  Morgan
Stanley may deal as principal in or own or act as market maker for
securities/instruments mentioned or may advise the issuers.  This is not
research and is not from MS Research but it may refer to a research
analyst/research report.  Unless indicated, these views are the author's
and may differ from those of Morgan Stanley research or others in the
Firm.  We do not represent this is accurate or complete and we may not
update this.  Past performance is not indicative of future returns.  For
additional information, research reports and important disclosures,
contact me or see https://secure.ms.com/servlet/cls.  You should not use
e-mail to request, authorize or effect the purchase or sale of any
security or instrument, to send transfer instructions, or to effect any
other transactions.  We cannot guarantee that any such requests received
via e-mail will be processed in a timely manner.  This communication is
solely for the addressee(s) and may contain confidential information.
We do not waive confidentiality by mistransmission.  Contact me if you
do not wish to receive these communications.  In the UK, this
communication is directed in the UK to those persons who are market
counterparties or intermediate customers (as defined in the UK Financial
Services Authority's rules).
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to buy/sell the securities/instruments mentioned or an official confirmation.  Morgan Stanley may deal as principal in or own or act as market maker for securities/instruments mentioned or may advise the issuers.  This is not research and is not from MS Research but it may refer to a research analyst/research report.  Unless indicated, these views are the author's and may differ from those of Morgan Stanley research or others in the Firm.  We do not represent this is accurate or complete and we may not update this.  Past performance is not indicative of future returns.  For additional information, research reports and important disclosures, contact me or see https://secure.ms.com/servlet/cls.  You should not use e-mail to request, authorize or effect the purchase or sale of any security or instrument, to send transfer instructions, or to effect any other transactions.  We cannot guarantee that any such requests received via e-mail will be processed in a timely manner.  This communication is solely for the addressee(s) and may contain confidential information.  We do not waive confidentiality by mistransmission.  Contact me if you do not wish to receive these communications.  In the UK, this communication is directed in the UK to those persons who are market counterparties or intermediate customers (as defined in the UK Financial Services Authority's rules).



More information about the R-help mailing list