Question

I am getting an error in R and am unsure how to correct it. I am getting an error from the r2 line, "Error in xc[1:(n - 2)] : only 0's may be mixed with negative subscripts."

tmpFn <- function(xVec)
{
xc <- xVec - mean(xVec)
denom <- sum(xc^2)
n <- length(x)
r1 <- sum( xc[2:n] * xc[1:(n-1)] )/denom
r2 <- sum( xc[3:n] * xc[1:(n-2)] )/denom
list(r1 = r1, r2 = r2)
}

tmpFn(seq(2, 56, 3))

10. (a) Given a vector (x1, ..., xn), the sample autocorrelation of lag k is defined to be Tk = i=k+1(Xi - &)(x;_k- Thus SL(X

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I ran your code and it is running perfectly fine. Just changed the 3rd line of the function as shown.

tmpFn <- function(xVec)
{
xc <- xVec - mean(xVec)
denom <- sum(xc^2)
n <- length(xVec)
r1 <- sum( xc[2:n] * xc[1:(n-1)] )/denom
r2 <- sum( xc[3:n] * xc[1:(n-2)] )/denom
list(r1 = r1, r2 = r2)
}
tmpFn(seq(2, 56, 3))

I got the output as

$r1
[1] 0.8421053

$r2
[1] 0.6859649

Add a comment
Know the answer?
Add Answer to:
I am getting an error in R and am unsure how to correct it. I am getting an error from the r2 line, "Error in xc[1:(...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C#, I am getting error placing them all in one file pls set them in the...

    C#, I am getting error placing them all in one file pls set them in the order that all 3 work in one file. Thanks //Program 1 using System; class MatrixLibrary { public static int[,] Matrix_Multi(int[,] a, int [,]b){ int r1= a.GetLength(0); int c1 = a.GetLength(1); int c2 = b.GetLength(1); int r2 = b.GetLength(0); if(r2 != c2){ Console.WriteLine("Matrices cannot be multiplied together.\n"); return null;    }else { int[,] c = new int[r1, c2]; for (int i = 0; i <...

  • I am trying to plot a time series model using R. So far I have the...

    I am trying to plot a time series model using R. So far I have the following code, but I'm getting an error located at the very bottom. Can you please explain what I'm doing wrong, and what I should be doing instead in order to avoid the error? As far as the data is concerned, the first column displays time (hour-by-hour) across 20 days, giving 504 rows of data and the six columns thereafter display the bike demand/usage at...

  • 2dddd0100bb/8216135?response- * LA89 VECTORS (PART 2) R 1Ri1=4 2) Keep in mind that Ř, and Ra...

    2dddd0100bb/8216135?response- * LA89 VECTORS (PART 2) R 1Ri1=4 2) Keep in mind that Ř, and Ra can be represented by 60 fix Ano TR1=4 60 X To their respective andy Components. Hence, if we add x, and a we will have На хотелот 2 к. 4 г. andef whold of, and I, care will have the y component of R1+R2. 3) add all & components and all IR, I means the magnitude o R We will now add R and...

  • I need help solving this problem. Thanks in advance. If correct, I will leave a like...

    I need help solving this problem. Thanks in advance. If correct, I will leave a like and a comment! Practice Problem 18.3 Constants Now let's look at the potential energy of a charge that is in the vicinity of two other charges. Suppose two electrons are held in place 10.0 cm apart. Point a is midway between the two electrons, and point b is 12.0 cm directly above point a. (a) Calculate the electric potential at point a and at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT