Question

"In R, define a function counting the values that are less than 0 for two vectors...

"In R, define a function counting the values that are less than 0 for two vectors x=rnorm(50) and y=rnorm(5000) respectively. Define another function to calcute the proportion that values are less than 0 for x and y respectively. Compare calculated proportions with theoretical proportion 0.5, what conclusions can you make?"

Please help me with the source code! Thanks!  

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

R-Code

X=rnorm(50)

length(X)

Z=as.vector(Z)

# Here the Z vector is created now we count the value in X which are less than 0

# Here the R vector is created now we count the value in Y which are less than 0

#In Loop vector Z is created such that if X is less than 0 the value is added. so the last value of this vector is the total count of value in X which are less than 0ifelse(X[1]<0,Z[1]+1,Z[1]+0)

for(i in 2:50)

{

if(X[i]<0)

Z[i]=Z[i-1]+1

else

Z[i]=Z[i-1]

}

Z

Y=rnorm(5000)

R=as.vector(R)

# Here the R vector is created now we count the value in Y which are less than 0

#In Loop vector R is created such that if Y is less than 0 the value is added. so the last value of this vector is the total count of value in Y which are less than 0

ifelse(Y[1]<0,R[1]+1,R[1]+0)

for(i in 2:5000)

{

if(Y[i]<0)

R[i]=R[i-1]+1

else

R[i]=R[i-1]

}

#Here the last value from Z & R is the count of value whcih are less than 0 in X & Y respectively

# The proportion is n/N

p1=Z[50]/50

p2=R[5000]/5000

p1

p2

# Test for Proportion

#H0: p1=0.5 Vs. H1 :p1 not equal to 0.5

#H0:p2=0.5 Vs. H1 :p2 not equal to 0.5

sd=sqrt(0.5*(1-0.5)/50)

Test1=(p1-0.5)/sd

Test1

Test2=(p2-0.5)/sd

Test2

#Calculate p-value to make decision

Prob1=pnorm(Test1,0,1)

Prob2=pnorm(Test2,0,1)

The conclusion is based on pvalue for the test ad it is dependent on X & Y. and Here X & Y are random so the test statistic change every time. The output is of when the program is run.

Run the given code

Output

> X=rnorm(50)

> length(X)

[1] 50

> Z=as.vector(Z)

> # Here the Z vector is created now we count the value in X which are less than 0

> # Here the R vector is created now we count the value in Y which are less than 0

> #In Loop vector Z is created such that if X is less than 0 the value is added. so the last value of this vector is the total count of value in X which are less than 0ifelse(X[1]<0,Z[1]+1,Z[1]+0)

> for(i in 2:50)

+ {

+ if(X[i]<0)

+ Z[i]=Z[i-1]+1

+ else

+ Z[i]=Z[i-1]

+ }

> Z

[1] 0 1 1 2 2 3 3 4 4 5 6 7 8 8 9 9 10 10 10 11 11 11 11 12

[25] 12 13 14 15 15 15 16 16 17 17 17 18 19 20 21 21 21 22 23 24 24 24 25 25

[49] 25 25

> Y=rnorm(5000)

> R=as.vector(R)

> # Here the R vector is created now we count the value in Y which are less than 0

> #In Loop vector R is created such that if Y is less than 0 the value is added. so the last value of this vector is the total count of value in Y which are less than 0

> ifelse(Y[1]<0,R[1]+1,R[1]+0)

[1] 2

> for(i in 2:5000)

+ {

+ if(Y[i]<0)

+ R[i]=R[i-1]+1

+ else

+ R[i]=R[i-1]

+ }

> #Here the last value from Z & R is the count of value whcih are less than 0 in X & Y respectively

> # The proportion is n/N

> p1=Z[50]/50

> p2=R[5000]/5000

> p1

[1] 0.5

> p2

[1] 0.5014

>

> # Test for Proportion

> #H0: p1=0.5 Vs. H1 :p1 not equal to 0.5

> #H0:p2=0.5 Vs. H1 :p2 not equal to 0.5

> sd=sqrt(0.5*(1-0.5)/50)

> Test1=(p1-0.5)/sd

> Test1

[1] 0

> Test2=(p2-0.5)/sd

> Test2

[1] 0.01979899

> #Calculate p-value to make decision

> Prob1=pnorm(Test1,0,1)

> Prob2=pnorm(Test2,0,1)

> Prob1

[1] 0.5

> Prob2

[1] 0.5078981

Here for both X & Y p-value is >0.05. so we fail to reject null hypothesis than proportion is 0.5

Add a comment
Know the answer?
Add Answer to:
"In R, define a function counting the values that are less than 0 for two vectors...
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
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