Please write a function to compute the expected value and standard deviation of an array of values. Compare your results with that of R’s mean and std functions.
Consider that instead of being able to neatly fit the values in memory in an array, you have an infinite stream of numbers coming by. How would you estimate the mean and standard deviation of such a stream? Your function should be able to return the current estimate of the mean and standard deviation at any time it is asked. Your program should maintain these current estimatees and return them back at any invocation of these functions. (Hint: You can maintain a rolling estimate of the mean and standard deviation and allow these to slowly change over time as you see more and more new values.)
#mean function
myMean<-function(x){
y<-sum(x)/length(x)
return(y)
}
#sd function
mySD<-function(x){
z<-sqrt(sum((x-myMean(x))^2)/(length(x)-1))
return(z)
}
#compare my function against R Base
mean(c(4,5,7,7,5,5))## [1] 5.5
myMean(c(4,5,7,7,5,5))## [1] 5.5
sd(c(4,5,7,7,5,5))## [1] 1.224745
mySD(c(4,5,7,7,5,5))## [1] 1.224745
random1000<- data.frame(time= integer(0), value = numeric(0), mean= numeric(0), sd = numeric(0))
sdStreamGraphic<-function(df){
i = 1
#create an infinite loop
repeat {
#generate a random value
newVal<-runif(1,1,1000)
df[i,1]<-i
df[i,2]<-newVal
df[i,3]<-myMean(df[,2])
df[i,4]<-mySD(df[,2])
flush.console()
plot(df$time,df$sd,
type='l',
main="Simulated Data Stream",
ylim=c(0,1000),
xlim=c(i-50,i),
xlab="Simulated Time",
ylab="Standard Deviation",
col=2,
lwd=2.5)
lines(df$time,1000-df$sd,col=2,type = 'l',lwd=2.5)
points(df$time, df$value,type = "p",pch="+")
lines(df$time,df$mean,col=3,type = 'l',lwd=2.5)
text(i-1,df[i,3], signif(df[i,3],5))
text(i-1,df[i,4], signif(df[i,4],5))
text(i-1,1000-df[i,4], signif(1000-df[i,4],5))
legend('topright', names(df)[-1:-2] ,
lty=1, lwd=2.5,col=c('green', 'red'), bty='n', cex=.75)
abline(h=500, col = "lightgray", lty=3)
Sys.sleep(.09)
i<-i+1
}
}
#run the function
sdStreamGraphic(random1000)