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
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...
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...
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')
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 {...
Solve the separable equation 3xy^2 dy/dx = x^2 + 1 given y(0) =5 I am getting...
Solve the separable equation 3xy^2 dy/dx = x^2 + 1 given y(0) =5 I am getting a general solution of cubed root x2/2+ln|x|+3c1 when I enter the initial conditions y(0)=5 the equation is undefined. How do deal with this? What would the final answer be? Thanks
This is like my 3rd time re asking this question because later I figured out 2...
This is like my 3rd time re asking this question because later I figured out 2 typos which is the P-value is 0.0000 and i can't get it to space where you see it off from the left of the anova table, and I missed putting a minus sign on part f. So thats why you keep seeing a duplicate questions from me. 1. A scientist study the relation of x=salinity level and y=nitrate level. He fitted a simple linear...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
Hello, I am having trouble getting my files to work together for this problem. Here is...
Hello, I am having trouble getting my files to work together for this problem. Here is the question and thank you for taking the time to answer it :) (1) Write an application that displays a menu of three items for the Jivin’ Java Coffee Shop as follows: American 1.99 Expresso 2.50 Latte 2.15 Prompt the user to choose an item using the number (1, 2, or 3) that corresponds to the item, or to enter 0 to quit the...
I am using matlab and getting a "matrix dimensions error below" for line 22. Can someone...
I am using matlab and getting a "matrix dimensions error below" for line 22. Can someone spot the error and try the code to fix the error. %The beginning step is to generate a functionf(t) that consists of the sum %of the following components % 25 Hz cosine function of magnitude 1 % 50 Hz sine function of magnitude 1 % 40 Hz square wave function of magnitude 1 clear; clc; close all; %sample rate is given at 2500 Hz...