2015-08-13 58 views
0

即時轉換R腳本爲JavaScript,以便我可以動態更改HightCharts.js圖。然而,我不知道如何編寫JavaScript函數來模擬R中的rnorm()函數。任何有關解決這個問題的建議都是非常值得讚賞的。將R轉換爲javascript用於highcharts.js

這是R代碼..

## create random normal variables with correct mean and standard deviations 

RHT <- round(rnorm(50, mean= HTot, sd= HtX)) 
RAT <- round(rnorm(50, mean= ATot , sd= AtX)) 

## combine data 

ranScor <- cbind(RHT,RAT) 
ranScor <- data.frame(ranScor) 

### plotting simple line graph 

plot(RHT,type="b", col= "blue", lwd=1, ylim=c(min(ranScor),max(ranScor)),xlab="Game Simulations",ylab="Team Score", main="Random Score Generator", sub="Indianapolis Colts @ Denver Broncos") 
lines(RAT, type="b", col="red",lwd=1) 
legend("topleft",legend=c("HomeTeam","AwayTeam"),lty=1,col=c("blue","red"),lwd=2,cex=.7) 
+0

https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform –

+0

你可以仔細閱讀標準正常R源之中,有是一些算法,https://github.com/wch/r-source/blob/trunk/src/nmath/snorm.c – jenesaisquoi

+0

[highchartR](https://github.com/jcizel/highchartR) – hrbrmstr

回答

0

你爲什麼不只是使用正態分佈的方程?生成的X和Y座標,然後繪製正態曲線:

function y(x, sd, mean) { 
    return Math.pow(Math.E,-Math.pow(x-mean,2)/(2*Math.pow(1, 2)))/(sd*Math.sqrt(2*Math.PI)); 
}