versioninfo()
Julia Version 1.0.1 Commit 0d713926f8 (2018-09-29 19:05 UTC) Platform Info: OS: Windows (x86_64-w64-mingw32) CPU: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-6.0.0 (ORCJIT, haswell) Environment: JULIA_CMDSTAN_HOME = C:\CmdStan JULIA_NUM_THREADS = 4 JULIA_PKGDIR = C:\JuliaPkg
#= To Install RCall:
using Pkg
Pkg.add("RCall")
=#
using RCall
X = randn(3,2)
b = reshape([2.0, 3.0], 2,1)
y = X * b + randn(3,1)
3×1 Array{Float64,2}: -4.201556132429842 0.4872545860474999 -1.0317679215996876
# Note that X and y are julia variables. @rput sends y and X to R with the same names
@rput y
@rput X
3×2 Array{Float64,2}: -2.01802 0.478014 -0.630687 0.394634 -0.597286 0.372121
# Fit a model in R
R"mod <- lm(y ~ X-1)"
R"summary(mod)"
RObject{VecSxp} Call: lm(formula = y ~ X - 1) Residuals: 1 2 3 0.001616 0.739294 -0.786096 Coefficients: Estimate Std. Error t value Pr(>|t|) X1 3.108 1.148 2.707 0.225 X2 4.329 3.490 1.240 0.432 Residual standard error: 1.079 on 1 degrees of freedom Multiple R-squared: 0.9386, Adjusted R-squared: 0.8157 F-statistic: 7.639 on 2 and 1 DF, p-value: 0.2479
# If you want to retrieve a variable from R, look at this example
R"z <- y * 3"
@rget z
z
3×1 Array{Float64,2}: -12.604668397289526 1.4617637581424996 -3.0953037647990627
# In this example, we really need to use reval
R_lm(some_str_expr) = reval(rparse("lm(" * some_str_expr * ")"))
R_summary = R"summary"
R_mod = R_lm("y ~ X-1")
R_summary(R_mod)
RObject{VecSxp} Call: lm(formula = y ~ X - 1) Residuals: 1 2 3 0.001616 0.739294 -0.786096 Coefficients: Estimate Std. Error t value Pr(>|t|) X1 3.108 1.148 2.707 0.225 X2 4.329 3.490 1.240 0.432 Residual standard error: 1.079 on 1 degrees of freedom Multiple R-squared: 0.9386, Adjusted R-squared: 0.8157 F-statistic: 7.639 on 2 and 1 DF, p-value: 0.2479
# Finally, plotting. Note that if you don't set ylab and xlab, you'll get a very messy plot...
R_plot = R"plot"
R_plot(x=rand(100),y=rand(100),pch=20,cex=2,fg="grey",bty="n",ylab="",xlab="")
R"plot(X[,1],y)"
# Now, you can call R_plot in julia quite like you call plot in R
RObject{NilSxp} NULL
R"plot(rnorm(2000))"
RObject{NilSxp} NULL
R_plot = R"plot";
R_plot(x=randn(2000),ylab="")
RObject{NilSxp} NULL
R"""
library(ggplot2)
str(iris)
"""
'data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
RObject{NilSxp} NULL
# http://motw.mods.jp/R/ggplot_facet.html
R"""
p <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width))
p + geom_point(colour="gray50", size=3) + geom_point(aes(colour=Species))
"""
RObject{VecSxp}
# http://motw.mods.jp/R/ggplot_geom_histogram.html
R"""
p <- ggplot(iris, aes(x=Sepal.Length))
p + geom_histogram(aes(colour=Species),bins=30)
"""
RObject{VecSxp}
R"""
p + geom_histogram(aes(fill=Species),bins=30)
"""
RObject{VecSxp}
R"""
str(diamonds)
"""
Classes 'tbl_df', 'tbl' and 'data.frame': 53940 obs. of 10 variables: $ carat : num 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ... $ cut : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ... $ color : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ... $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ... $ depth : num 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ... $ table : num 55 61 65 58 58 57 57 55 61 61 ... $ price : int 326 326 327 334 335 336 336 337 337 338 ... $ x : num 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ... $ y : num 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ... $ z : num 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
RObject{NilSxp} NULL
# http://motw.mods.jp/R/ggplot_facet.html
R"""
p <- ggplot(diamonds, aes(x=carat, y=price))
p + geom_point(aes(colour=clarity)) + facet_grid(cut ~ color, margins=TRUE)
"""
RObject{VecSxp}
# https://mrunadon.github.io/ggplot2/
R"""
g3<-ggplot(iris,aes(x=Sepal.Length)) #確率密度曲線を描いていきたいので、y軸の宣言は不要です。確率密度がy軸になるので。
g3<-g3+geom_density(aes(fill=Species),size=0.5,alpha=0.5) #geom_density()を使います
g3<-g3+xlim(3,10) #x軸は3から10の範囲としましょう。
g3
"""
RParseError: unicode script is not supported Stacktrace: [1] render(::String) at C:\Users\genkuroki\.julia\packages\RCall\Q4n8R\src\render.jl:14 [2] @R_str(::LineNumberNode, ::Module, ::Any) at C:\Users\genkuroki\.julia\packages\RCall\Q4n8R\src\macros.jl:62
# https://mrunadon.github.io/ggplot2/
R"""
g3<-ggplot(iris,aes(x=Sepal.Length))
g3<-g3+geom_density(aes(fill=Species),size=0.5,alpha=0.5)
g3<-g3+xlim(3,10)
g3
"""
RObject{VecSxp}
R"hoge <- 3"
RObject{RealSxp} [1] 3
@rget hoge
3.0
moge = Float64(pi)
3.141592653589793
@rput moge
3.141592653589793
R"moge"
RObject{RealSxp} [1] 3.141593
poge = [
1 2 3;
4 5 6;
7 8 9;
]
3×3 Array{Int64,2}: 1 2 3 4 5 6 7 8 9
@rput poge
3×3 Array{Int64,2}: 1 2 3 4 5 6 7 8 9
R"poge"
RObject{IntSxp} [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9
pogecopy = deepcopy(poge)
3×3 Array{Int64,2}: 1 2 3 4 5 6 7 8 9
R"poge <- 2*poge"
RObject{RealSxp} [,1] [,2] [,3] [1,] 2 4 6 [2,] 8 10 12 [3,] 14 16 18
@rget poge
3×3 Array{Float64,2}: 2.0 4.0 6.0 8.0 10.0 12.0 14.0 16.0 18.0
poge
3×3 Array{Float64,2}: 2.0 4.0 6.0 8.0 10.0 12.0 14.0 16.0 18.0
pogecopy
3×3 Array{Int64,2}: 1 2 3 4 5 6 7 8 9