Show me how to write the script in r. What we need to do is to create a for loop
ID: 3576320 • Letter: S
Question
Show me how to write the script in r. What we need to do is to create a for loop to get x and p from the table to all at once get placed into the mymean function and mysd function. i.e.) x1 = -1; x2 = 2; x3 = 5; x4 = 9.4; and p1 = 0.1; p2 = 0.3; p3 = 0.2; p4 = 0.4. For i = 1:4, x = xi, p = pi, mymean(x,p), mysd(x,p). Then these 2 functions output the values. I am not sure how to write the script in r. So please help me. Thank you.
In this problem we will write functions to find mean and standard deviation of discrete random variables. For a random variable X, I T1, 12,... am) is the vector of values, i e., X takes the values (I1, I2, ,In). p 1, p2, pn) is the vector of probabilities i.e. pi POX Ii) You have to write two functions mymean and mysd to find the mean and T pO X standard deviation respectively, i.e. mymean (x,p) will give the mean and -1 0.1 mysd x,p) will give the standard deviation of X. 2 0.3 5 0.2 9.4 0.4Explanation / Answer
mydata<-function(y1,y2,y3,y4,n1param = true,print=True){
if(!n1param){
centre <-mean(y);
}
else{
spread=sd(y);
}
if(print & !n1param)
{
cat("mean=", centre )
}
else{
cat("sd=" , spread)
}
result <-list(center= center,spread= spread)
return(result)
}
x1 <- rpois(-1,0.1)
x2<-rpois(2,0.3)
x3<-rpois(5,0.2)
x4<-rpois(9.4,0.4)
y<-mydata(x1,x2,x3,x4);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.