Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

r sagews demos

Project: 🐪 SRE
Views: 3842

R Basics

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

%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
4
%r z <- sample(-5:5, 30, replace=T) z summary(z)
  1. 2
  2. 0
  3. -5
  4. 2
  5. -2
  6. 1
  7. 2
  8. 1
  9. -2
  10. 0
  11. 1
  12. -5
  13. 4
  14. 0
  15. -5
  16. 4
  17. 0
  18. -2
  19. -3
  20. 2
  21. 0
  22. -3
  23. 3
  24. -4
  25. 0
  26. 2
  27. -5
  28. 3
  29. 1
  30. 0
Min. 1st Qu. Median Mean 3rd Qu. Max. -5.0000 -2.0000 0.0000 -0.2667 2.0000 4.0000
%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 -10.8981 -2.7457 0.1564 2.8784 11.6248 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -1.6343 0.4371 -3.739 0.000311 *** X 0.8292 0.1447 5.730 1.11e-07 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 4.355 on 98 degrees of freedom Multiple R-squared: 0.2509, Adjusted R-squared: 0.2433 F-statistic: 32.83 on 1 and 98 DF, p-value: 1.11e-07