Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Github repo cloud-examples: https://github.com/sagemath/cloud-examples

Views: 7912
License: MIT

R Basics

Just a demo, how R can be called from Sage to do something useful.

print r
R Interpreter
x = r.c((1,2,3,-1, 2,2.2)) print x type(x) print x.summary()
[1] 1.0 2.0 3.0 -1.0 2.0 2.2 <class 'sage.interfaces.r.RElement'> Min. 1st Qu. Median Mean 3rd Qu. Max. -1.000 1.250 2.000 1.533 2.150 3.000
%r x <- c(1,4,3,4,3,2,3,2,3,6,3) summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max. 1.000 2.500 3.000 3.091 3.500 6.000
%r x[1] x[2]
[1] 1 [1] 4
%r z <- sample(-5:5, 30, replace=T) z summary(z)
[1] 5 5 -2 5 -5 -4 -3 -4 1 -1 -1 -3 0 0 -2 1 -4 -2 -2 -2 -5 -5 -4 1 1 4 -2 -1 0 -4 Min. 1st Qu. Median Mean 3rd Qu. Max. -5.00 -3.75 -2.00 -1.10 0.75 5.00
z2 = r.sample(range(-5,5), 10, replace=False) z2
[1] 0 4 -1 -5 2 -2 -3 -4 3 1
%r plot(-5:5, sample(-5:5, 11, replace=F))

Linear Regression

%r X <- sort(runif(100, -5,5)) Y <- -2 + 1.1 * X + rnorm(100, 0, 5) lmobj <- lm(Y ~ X) summary(lmobj)
Call: lm(formula = Y ~ X) Residuals: Min 1Q Median 3Q Max -9.8958 -3.1144 -0.4075 2.8230 12.4266 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -1.6632 0.4948 -3.361 0.00111 ** X 1.3158 0.1904 6.909 4.94e-10 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 4.945 on 98 degrees of freedom Multiple R-squared: 0.3276, Adjusted R-squared: 0.3207 F-statistic: 47.74 on 1 and 98 DF, p-value: 4.941e-10