Question

This is an example out of my text which I copied directly but am getting an...

This is an example out of my text which I copied directly but am getting an error every iteration and am unsure how to correct it. The error is "

the condition has length > 1 and only the first element will be used" and my code is:

f <- function(x, sigma) {
if (any(x < 0)) return(0)
stopifnot(sigma > 0)
return((x/sigma^2)*exp(-x^2 / (s*sigma^2)))
}

xt <- x[i-1]
y <- rchisq(1, df = xt)

m <- 10000
sigma <- 4
x <- numeric(m)
x[1] <- rchisq(1, df = 1)
k <- 0
u <- runif(m)

for(i in 2:m) {
xt <- x[i-1]
y <- rchisq(1, df = xt)
num <- f(y, sigma) * dchisq(xt, df = y)
den <- f(xt, sigma) * dchisq(y, df = xt)
if (u[i] <= num/den) x[i] <- y else {
x[i] <- xt
k <- k+1
}
}
print(k)

index <- 5000:5500
y1 <- x[index]
#plot(index, y1, type = "l", main = "", ylab = "x")

Homework Answers

Answer #1

The following modified code gives one error:

Error in f(y, sigma) : object 's' not found

You are not sending the value of "s" to the function "f".

No other errors. Is it "s" or '2" ?

----------------------------------------------------------------------------------------

f <- function(x, sigma) {
if (any(x < 0)) return(0)
stopifnot(sigma > 0)
return((x/sigma^2)*exp(-x^2 / (s*sigma^2)))
}
m <- 10000
sigma <- 4
x <- numeric(m)
x[1] <- rchisq(1, df = 1)
k <- 0
u <- runif(m)

xt <- x[i-1]
y <- rchisq(1, df = xt)

for(i in 2:m) {
xt <- x[i-1]
y <- rchisq(1, df = xt)
num <- f(y, sigma) * dchisq(xt, df = y)
den <- f(xt, sigma) * dchisq(y, df = xt)
if (u[i] <= num/den) x[i] <- y else {
x[i] <- xt
k <- k+1
}
}
print(k)

index <- 5000:5500
y1 <- x[index]
#plot(index, y1, type = "l", main = "", ylab = "x")

Know the answer?
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
Similar Questions
I am not sure what I am doing wrong in this coding. I keep getting this...
I am not sure what I am doing wrong in this coding. I keep getting this error. I tried to change it to Circle2D.java but it still comes an error: Compiler error: class Circle2D is public, should be declared in a file named Circle2D.java public class Circle2D { public class Exercise10_11 { public static void main(String[] args) {     Circle2D c1 = new Circle2D(2, 2, 5.5);     System.out.println("area: " + c1.getArea());     System.out.println("perimeter: " + c1.getPerimeter());     System.out.println("contains(3, 3): "...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
This is my code, python. I have to search through the roster list to find a...
This is my code, python. I have to search through the roster list to find a player using their number. it says list index out of range. it also says there is error in my main. def file_to_dictionary(rosterFile): myDictionary={}       with open(rosterFile,'r') as f: data=f.read().split('\n')       for line in data:    (num,first,last,position)=line.split() myDict=[first, last, position] myDictionary[num]=myDict print (myDictionary) return myDictionary file_to_dictionary((f"../data/playerRoster.txt"))    def find_by_number(number): player=None    second=[] foundplayer= False myDictionary=file_to_dictionary((f"../data/playerRoster.txt")) for p in myDictionary: fullplayer=p.split() second.append([fullplayer[0], (fullplayer[1]+" "+...
Hello, I feel like I am super close but I can not figure out why my...
Hello, I feel like I am super close but I can not figure out why my C++ code it not displaying the proper medium. Thank you! Here is an example of the output: Input : a[] = {1, 3, 4, 2, 6, 5, 8, 7} Output : Mean = 4.5 Median = 4.5 Code so far:   #include <iostream> using namespace std; int main() { int a[100]; int n,i,sum=0; float mean, medium; //read array size // read array cout<<"Enter array size:...
Hello! I hope you are healthy and well! I am hoping that this message finds you...
Hello! I hope you are healthy and well! I am hoping that this message finds you happy and content! I am having trouble solving this 5-part practice problem. I would greatly appreciate any and all help that you could lend! Thanks in advance! In the following proof, what is the justification for line 7? 1.     [(W ⊃ X) ⊃ Y] ∨ ( P ≡ Q) 2.     ∼X • ∼Y 3.     ∼(P ≡ Q) / ∴ ∼W 4.     ∼X                  2 Simp 5.     ∼Y                  2 Simp 6.     (W ⊃ X)...
MATLAB** I'm not sure what I'm doing wrong writing out my equation. I keep getting an...
MATLAB** I'm not sure what I'm doing wrong writing out my equation. I keep getting an error. It has something to do with the line with the function written out. Here's what I have: %3.2 clc clear close all %plot ?(?,?)=?(1+sin?), as a 3D plot on the domain ?∈[−8,8], ?∈[−8,8]. x=[-8:8]; y=[-8,8]; z=x(sin(y)+1); plot3(x,y,z) title('Plot 3.2')
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
0 I am using putty ssh to import my csv file to Hadoop file system (HDFS)....
0 I am using putty ssh to import my csv file to Hadoop file system (HDFS). So far I have made a directory using the command hadoop fs -mdkir /data after the directory I am trying to import my csv file using command: hadoop fs -cp s3://cis4567-fall19/Hadoop/SalesJan2 009.csv However I am getting a error that states : -cp: Not enough arguments: expected 2 but got 1
Question 4 please although I am unsure of my answers for the rest as well :(...
Question 4 please although I am unsure of my answers for the rest as well :( Here is the full list for background info. Consider a significance test for a null hypothesis versus a two-sided alternative. State all values of a standard normal test statistic z that will give a result significant at the 10% level but not at the 5% level of significance. (Sec. 6.2) You perform 1,000 significance tests using α = 0.01. Assuming that all the null...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT