[R] Power Function

Joshua Wiley jwiley.psych at gmail.com
Sat Sep 25 04:20:20 CEST 2010


Hi,

What part of your code does not work or does not do what you want?  I
am guessing that you want gute to return more than a single value
(which is what it does in your code).  Can you show us an example of
the results you would like?

Best regards,

Josh



I reworked some of your code to simplify, clean, and wrap it all in a function:

myfun <- function(n, m, alpha = .05, seeder = 1000) {
  set.seed(seeder)
  x <- matrix(rnorm(n, 0, 0.5), ncol = m)
  y <- matrix(rnorm(n, 0, 0.8), ncol = m)
  l <- diag(cor(x, y))
  cat("Correlations between two random variables \n", l, fill = TRUE)
  gute <- function(x, m, alpha) {
    q_1 <- qnorm(alpha, 0, 0.05)
    q_2 <- qnorm(1 - alpha, 0, 0.05)
    p <- (x^2)/sum(x^2)
    H <- log(m) - sum(p * log(p), na.rm = TRUE)
    1 - mean(q_1 <= H & H <= q_2)
  }
  dat <- seq(0, 1, length.out = 10)
  output <- gute(x = dat, m = m, alpha = alpha)
  return(output)
}

This results in:

> myfun(100, 5)
Correlations between two random variables
 -0.5887829 0.07042411 -0.06082638
0.2395193 -0.1038213
[1] 1


On Fri, Sep 24, 2010 at 5:26 PM, jethi <kartija at hotmail.com> wrote:
>
> Hi, at first, i´m from germany, so sorry for my bad english. but i need ur
> help in R to programm a power function  and to make at last a graphik of it.
>
> i have already tried my best. but it doesn´t work.the topic is: the
> homogeneity test of correlation based entropy.
>
> so it means, that i have to check if all correlations of a bivariate random
> vectors are same or not. for that i saperate the  n bivariate random vectors
> (x1,y1),...,(xn,yn)  in blocks  m so, so that i at first calculate  the
> correlation in a  block. n=m*k. the numbers of the blocks m are
> user-defined. the test value is the entropy. pls help me!!!
>
>
> set.seed(1000)
> n=100
> m=5
> k=n/m
>
> x=rnorm(n,0,0.5)
> y=rnorm(n,0,0.8)
>
>
>  #alpha/2 Quantil
>  q_1=qnorm(0.05,0,0.05)
>
>  #1-alpha/2 Quantile
>  q_2=qnorm(0.95,0,0.05)
>
>
>  l=matrix(0,nrow=m,ncol=1)
> for(i in 1:m){
>
>
> l[i]=print(cor((x[(((i-1)*k)+1):(((i-1)*k)+k)]),
> (y[(((i-1)*k)+1):(((i-1)*k)+k)])))
>
>
> }
> güte=function(l){
> p=matrix(0,nrow=m,ncol=1)
> for(i in 1:m){
> p[i]=l[i]^2/sum(l^2)
>
> }
> H=log(m)-sum(p*log(p))
> 1-mean(q_1<=H & H <=q_2)
> }
>
>
> l=seq(0,1,len=10)
>
>
>
>
>
> plot(l,güte, type="o",pch=20,ylim=c(0,1),col="red")
> --
> View this message in context: http://r.789695.n4.nabble.com/Power-Function-tp2631929p2631929.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list