-3
require(fracdiff)
#load your data, n is the sample size

x<-matrix(scan("data.txt", n = ), n, 1, byrow = TRUE)

x<-log(x)
x<-x-mean(x)
n<-length(x)


#select the truncation
m<-round(n^(0.7))

perdx<-px[2:n]
fn<-function(h)
    {
        lambda<-freq[1:m]^(2*h-1)
        Gh=mean(perdx[1:m]*lambda)
        Rh=log(Gh)-(2*h-1)*mean(log(freq[1:m]))
        return(Rh)
    }
    est<-optimize(fn,c(0,1.5),tol = 0.00001)
    hhat<-est$minimum
    b <- hhat-0.5
    b

I have this function written in R and I want to do loop for m, where m<-round(n^(0.7)) where the power of n running from 0.3-0.8(the default is 0.7, so I have different value of m supply to the function), and ultimately get the string of b (every b for the power of n running from 0.3-0.8), but so far I have been unsuccessful. Furthermore, I want to plot the different values of b with respect to m. I'm really hoping anyone can suggest me how get the result. Any suggestion is highly appreciated. thank you.

  • 6
    Please [make the example reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by giving `freq` and `perdx`. – Roland Dec 11 '12 at 08:03
  • -1 because 1) This is still not reproducible; 2) You changed the parameters of the question after someone (me) answered; 3) Why `require(fracdiff)`? every function in your code is in `base`. – plannapus Dec 11 '12 at 08:47

1 Answers1

2

First some dummy data:

set.seed(1983)
freq <- rnorm(100, mean=10)
perdx <- rnorm(100, mean=100, sd=10)

Then your function (slightly shortened and modified: you need to add m as a parameter since it will be changing at each iteration), vector m and an empty vector b (you do want to allocate upfront the length of the vector):

m <- 1:100
b <- vector(length=length(m))
fn <- function(h,m){
    lambda <- freq[1:m]^(2*h-1)
    Gh <- mean(perdx[1:m]*lambda)
    log(Gh)-(2*h-1)*mean(log(freq[1:m]))
    }

And finally your loop:

for(i in seq_along(m)){b[i] <- optimize(fn,c(0,1.5),tol = 0.00001, m=m[i])$minimum - 0.5}

b
  [1]  0.370809995  0.143004969  0.295652288  0.341975500  0.155323568 -0.004270843 -0.004463482 -0.005151013 -0.019702428 -0.066622938 -0.071558276 -0.051269281 -0.010162819 -0.011613268
 [15] -0.043173232 -0.023702358 -0.017404588 -0.041314701 -0.041849379 -0.039659543 -0.042926431 -0.041149212 -0.050584172 -0.051101425 -0.051999900 -0.053473729 -0.007245899 -0.023556513
 [29] -0.026109458 -0.035935971 -0.063366257 -0.048185532 -0.051862241 -0.051659993 -0.078318886 -0.080683266 -0.082146068 -0.088776082 -0.095815094 -0.097276217 -0.099827675 -0.090215664
 [43] -0.091023273 -0.090649640 -0.091877778 -0.091318363 -0.083812376 -0.091700899 -0.086337626 -0.105456723 -0.105972890 -0.101094946 -0.101748039 -0.101323129 -0.070511638 -0.081105305
 [57] -0.072667430 -0.072361640 -0.069692202 -0.067384208 -0.072985712 -0.063617816 -0.064122242 -0.067135980 -0.070663150 -0.069359528 -0.069691113 -0.084422380 -0.073379583 -0.072209507
 [71] -0.069132825 -0.067681419 -0.063782326 -0.057532656 -0.063031479 -0.054001810 -0.053523184 -0.051783114 -0.053388449 -0.055742505 -0.052429781 -0.058399275 -0.059529803 -0.059389065
 [85] -0.058834476 -0.043061836 -0.045186752 -0.048336234 -0.055597368 -0.065307991 -0.060903775 -0.062518358 -0.062898386 -0.059452595 -0.051983381 -0.049742105 -0.050124722 -0.049212744
 [99] -0.041458672 -0.043251041
plannapus
  • 18,529
  • 4
  • 72
  • 94
  • You're welcome. I modified it slightly (from `for( in in 1:100)` to `for(i in 1:length(m))` so you can replace `m` by anything you want. Just be careful to keep `m` as a vector of integers since you will index `freq` and `perdx` with it. With your new example it will become: `m <- round(n^(seq(0.3,0.8, by=0.01)))` for example. – plannapus Dec 11 '12 at 09:03
  • 1
    You could use `seq_along(m)` instead of `1:length(m)`. – Roland Dec 11 '12 at 09:29
  • @Roland Indeed. And that comes handy when m is NULL (length=0). I just assumed, given the context, that it won't be. – plannapus Dec 11 '12 at 09:43