МЕТРОЛОГІЯ І СТАНДАРТИЗАЦІЯ
Повернутись на головну
Повернутись до завдання
Оновлення сторінки CTL + F5
Робота в программах R, RStudio
q()
demo()
help(mean)
c
"Hello World!"
5+5
for (x in 1:10) {
print(x)
}
while (i < 6) {
print(i)
i <- i + 1
}
i <- 1
while (i < 6) {
print(i)
i <- i + 1
}
> i <- 1
i <- 1
i
P <- 0
P
ro <-1000
g <- 9.81
g
H <- 3
H
while (i < 100) {
P <- P+ro*g*(H-i*H/100)*1*(H/100)
i <- i + 1
}
P
mP <- 0
while (i < 100) {
mP <- mP+ro*g*(H-i*H/100)*(i*H/100)*1*(H/100)
i <- i + 1
}
mP
i <- 1
mP <- 0
while (i < 100) {
mP <- mP+ro*g*(H-i*H/100)*(i*H/100)*1*(H/100)
i <- i + 1
}
mP
mP/P
mP <- 0
i <- 1
while (i < 100) {
mP <- mP+ro*g*(H-i*H/100)*(H-i*H/100)*1*(H/100)
i <- i + 1
}
mP
mP/P
savehistory("D:/jknuba_2023_end/TBKVM_temp/jh_R_history.Rhistory")
for (x in 1:10) {
print(x)
}
# Vector of characters/strings
fruits <- c("banana", "apple", "orange")
# Print fruits
fruits [3]
savehistory("D:/jknuba_2023_end/TBKVM_temp/jh_R_history1.Rhistory")
# https://www.w3schools.com/r/r_vectors.asp
savehistory("D:/jknuba_2023_end/TBKVM_temp/jh_R_history1.Rhistory")
x <- c(0:10, 50)
xm <- mean(x)
c(xm, mean(x, trim = 0.10))
x
mean(x)
x1 <- c(0:10)
x1
mean(x1)
x2 <- c(0:10,0,0,0,0)
x2
length(x2)
mean(x2)
hist(x2)
hist(x1)
mean(x1)
median(x1)
mean(x2)
median(x2)
rnorm(100, mean = 55, sd = 0.3)
x4 <- rnorm(100, mean = 55, sd = 0.3)
x4
hist(x4)
mean(x4)
median(x4)
utils:::menuInstallPkgs()
utils:::menuInstallPkgs()
utils:::menuInstallPkgs()
local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
if(nchar(pkg)) library(pkg, character.only=TRUE)})
utils:::menuInstallPkgs()
utils:::menuInstallPkgs()
update.packages(ask='graphics',checkBuilt=TRUE)
ls()
q()
install.packages(c("codetools", "effectsize", "ggstatsplot", "htmltools", "lme4", "munsell"))
print("hello world")
sqrt(2)
print("hello world")
print(a)
a <- "hello world"
print(a)
install.packages("ggplot2")
library(ggplot2)
library(palmerpenguins)
install.packages("palmerpenguins")
library(palmerpenguins)
qplot(x = bill_length_mm, data = penguins)
qplot(x = bill_length_mm, data = penguins, y = bill_depth_mm)
penguins
print(n = ...)
print(n = 11..20)
print(n = 20)
install.packages("installr")
updateR()
help("mean")
savehistory("D:/jknuba_2024_winter/j_AME/jR_history.Rhistory")
age <- c(1, 3, 5, 2, 11, 9, 3, 9, 12, 3)
xm <- mean(age)
xm
median(age)
?sd
sd(age)
var(age)
str(age)
stat.desc(age)
library(pastecs)
summary(age)
?hist
hist(age)
descr(age)
?descr
library(palmerpenguins)
p <- c(913.809599612895,195.184617797788,482.390524851619,355.440057159286,252.23887668538,415.562722721482,612.573889253824,841.812655335921,505.589444643811,678.461995018514,36.393315899922,625.297518341807,993.731457869481,41.58924615445,632.625389366601,593.152181516976,654.657766114507,470.710674033664,51.923553664518,2.327880375314,315.607696285911,860.229526244811,42.065476540857,848.669942849393,232.977560744436,691.114194622159)
p
h <- mean(p)
h
sd(p)
max(p)
min(p)
range(p)
var(p)
hist(p)
mouse.data <- data.frame(
weight=c(0.9, 1.8, 2.4, 3.5, 3.9, 4.4, 5.1, 5.6, 6.3),
size=c(1.4, 2.6, 1.0, 3.7, 5.5, 3.2, 3.0, 4.9, 6.3))
mouse.data # print the data to the screen in a nice format
## plot a x/y scatter plot with the data
plot(mouse.data$weight, mouse.data$size)
## create a "linear model" - that is, do the regression
mouse.regression <- lm(size ~ weight, data=mouse.data)
## generate a summary of the regression
summary(mouse.regression)
## add the regression line to our x/y scatter plot
abline(mouse.regression, col="blue")
## Here's the data
mouse.data <- data.frame(
size = c(1.4, 2.6, 1.0, 3.7, 5.5, 3.2, 3.0, 4.9, 6.3),
weight = c(0.9, 1.8, 2.4, 3.5, 3.9, 4.4, 5.1, 5.6, 6.3),
tail = c(0.7, 1.3, 0.7, 2.0, 3.6, 3.0, 2.9, 3.9, 4.0))
mouse.data
#######################################################
##
## Let's start by reviewing simple regression by
## modeling mouse size with mouse weight.
##
#######################################################
## STEP 1: Draw a graph of the data to make sure the relationship make sense
plot(mouse.data$weight, mouse.data$size, pch=16, cex=2)
## STEP 2: Do the regression
simple.regression <- lm(size ~ weight, data=mouse.data)
## STEP 3: Look at the R^2, F-value and p-value
summary(simple.regression)
abline(simple.regression, lwd=5, col="red")
## now let's verify that our formula for R^2 is correct..
ss.mean <- sum((mouse.data$size - mean(mouse.data$size))^2)
ss.simple <- sum(simple.regression$residuals^2)
(ss.mean - ss.simple) / ss.mean # this is the R^2 value
## now let's verify the our formula for F is correct...
f.simple <- ((ss.mean - ss.simple) / (2 - 1)) /
(ss.simple / (nrow(mouse.data) - 2))
f.simple # this is the F-value
## Now let's draw a figure that shows how to calculate the p-value from the
## F-value
##
## First, draw the correct f-distribution curve with df1=1 and df2=7
x <- seq(from=0, to=15, by=0.1)
y <- df(x, df1=1, df2=7)
plot(x, y, type="l")
## now draw a verticle line where our F-value, f.simple, is.
abline(v=f.simple, col="red")
## color the graph on the left side of the line blue
x.zero.to.line <- seq(from=0, to=f.simple, by=0.1)
y.zero.to.line <- df(x.zero.to.line, df1=1, df2=7)
polygon(x=c(x.zero.to.line, 0), y=c(y.zero.to.line, 0), col="blue")
## color the graph on the right side of the line red
x.line.to.20 <- seq(from=f.simple, to=20, by=0.1)
y.line.to.20 <- df(x.line.to.20, df1=1, df2=7)
polygon(x=c(x.line.to.20, f.simple), y=c(y.line.to.20, 0), col="red")
pf(f.simple, df1=1, df2=7) ## the area under the curve that is blue
1-pf(f.simple, df1=1, df2=7) ## the area under the curve that is red
## lastly, let's compare this p-value to the one from the
## original regression
summary(simple.regression)
#######################################################
##
## Now let's do multiple regression by adding an extra term, tail length
##
#######################################################
## STEP 1: Draw a graph of the data to make sure the relationship make sense
## This graph is more complex because it shows the relationships between all
## of the columns in "mouse.data".
plot(mouse.data)
## STEP 2: Do the regression
multiple.regression <- lm(size ~ weight + tail, data=mouse.data)
## STEP 3: Look at the R^2, F-value and p-value
summary(multiple.regression)
## again, we can verify that our R^2 value is what we think it is
ss.multiple <- sum(multiple.regression$residuals^2)
(ss.mean - ss.multiple) / ss.mean
## we can also verify that the F-value is what we think it is
f.multiple <- ((ss.mean - ss.multiple) / (3 - 1)) /
(ss.multiple / (nrow(mouse.data) - 3))
f.multiple
## Again let's draw a figure that shows how to calculate the p-value from the
## F-value
##
## First, draw the correct f-distribution curve with df1=2 and df2=6
x <- seq(from=0, to=20, by=0.1)
y <- df(x, df1=2, df2=6)
plot(x, y, type="l")
## now draw a verticle line where our f.value is for this test
abline(v=f.multiple, col="red")
## color the graph on the left side of the line blue
x.zero.to.line <- seq(from=0, to=f.multiple, by=0.1)
y.zero.to.line <- df(x.zero.to.line, df1=2, df2=6)
polygon(x=c(x.zero.to.line, 0), y=c(y.zero.to.line, 0), col="blue")
## color the graph on the right side of the line red
x.line.to.20 <- seq(from=f.multiple, to=20, by=0.1)
y.line.to.20 <- df(x.line.to.20, df1=2, df2=6)
polygon(x=c(x.line.to.20, f.multiple), y=c(y.line.to.20, 0), col="red")
pf(f.multiple, df1=2, df2=6) ## the area under the curve that is blue
1-pf(f.multiple, df1=2, df2=6) ## the area under the curve that is red
## lastly, let's compare this p-value to the one from the
## original regression
summary(multiple.regression)
#######################################################
##
## Now, let's see if "tail" makes a significant contribution by
## comparing the "simple" fit (which does not include the tail data)
## to the "multiple" fit (which has the extra term for the tail data)
##
#######################################################
f.simple.v.multiple <- ((ss.simple - ss.multiple) / (3-2)) /
(ss.multiple / (nrow(mouse.data) - 3))
1-pf(f.simple.v.multiple, df1=1, df2=6)
## Notice that this value is the same as the p-value next to the term for
## for "tail" in the summary of multiple regression:
summary(multiple.regression)
## Thus, the summary already calculated this F-value and p-value for us.
## this line tells us that including the "tail" term makes a statistically
## significant difference. The magnitude can be determined by looking
## at the change in R^2 between the simple and multiple regressions.
library(ggplot2)
library(cowplot)
library(randomForest)
install.packages(c("backports", "bayestestR", "boot", "brio", "broom", "cachem", "checkmate", "cli", "colorspace", "correlation", "corrplot", "cpp11", "crayon", "curl", "datawizard", "digest", "effectsize", "evaluate", "farver", "fastmap", "foreign", "fs", "ggplot2", "ggsci", "ggstatsplot", "gmp", "gtable", "insight", "KernSmooth", "lme4", "magick", "matrixStats", "minqa", "mvtnorm", "nlme", "nloptr", "parameters", "pbkrtest", "performance", "pkgload", "prismatic", "ps", "psych", "quantreg", "Rcpp", "RcppEigen", "rlang", "SparseM", "statsExpressions", "stringi", "survival", "testthat", "waldo", "withr"))
## Here's the data from the video
mouse.data <- data.frame(
weight=c(0.9, 1.8, 2.4, 3.5, 3.9, 4.4, 5.1, 5.6, 6.3),
size=c(1.4, 2.6, 1.0, 3.7, 5.5, 3.2, 3.0, 4.9, 6.3))
mouse.data # print the data to the screen in a nice format
## plot a x/y scatter plot with the data
plot(mouse.data$weight, mouse.data$size)
## create a "linear model" - that is, do the regression
mouse.regression <- lm(size ~ weight, data=mouse.data)
## generate a summary of the regression
summary(mouse.regression)
## add the regression line to our x/y scatter plot
abline(mouse.regression, col="blue")
x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
x
mean (x)
median(x)
quantile(x)
mode(x)
median(x)
randu(x)
randu
x
y
z
c <-randu
c
c.x
library(ggplot2)
library(palmerpenguins)
qplot(x = bill_length_mm, data = penguins, y = bill_depth_mm)
5+5
help(mean)
for (x in 1:10) {
print(x)
}
while (i < 6) {
print(i)
i <- i + 1
}
i <- 1
while (i < 6) {
print(i)
i <- i + 1
}
P <- 0
P
ro <-1000
g <- 9.81
g
H <- 3
H
while (i < 100) {
P <- P+ro*g*(H-i*H/100)*1*(H/100)
i <- i + 1
}
P
p <- (913.809599612895, 195.184617797788 , 482.390524851619 , 355.440057159286 , 252.238876685380 , 415.562722721482 , 612.573889253824 , 841.812655335921 , 505.589444643811 , 678.461995018514 , 36.393315899922 , 625.297518341807 , 993.731457869481 , 41.589246154450 , 632.625389366601 , 593.152181516976 , 654.657766114507 , 470.710674033664 , 51.923553664518 , 2.327880375314 , 315.607696285911 , 860.229526244811 , 42.065476540857 , 848.669942849393 , 232.977560744436 , 691.114194622159 , 611.555808193656 , 871.141807772655 , 151.744513967412 , 636.167689501466 , 658.532815977176 , 590.621978600353 , 468.446995661718 , 770.847364589109 , 704.552743083740 , 515.772651155304 , 748.603529766676 , 320.051495646933 , 452.706289503140 , 128.212007947998 , 672.610701317176 , 16.369229849299 , 782.480016247550 , 263.366503906158 , 162.835299370387 , 923.580636753458 , 82.324452959251 , 121.483465493213 , 903.144559254483 , 647.080187498739 , 851.334835076406 , 544.299773886374 , 43.072371576313 , 664.077913867694 , 910.449141500167 , 964.560030330705 , 99.114379638628 , 198.372211824154 , 286.807618396517 , 801.876281558936 , 823.581458346424 , 163.851981253729 , 543.221956212087 , 282.045077397080 , 284.316912558798 , 598.551711360379 , 403.866336074384 , 51.051272034111 , 719.240330976492 , 672.711617954491 , 984.476891247987 , 708.164457590273 , 110.667051586940 , 456.262820744485 , 806.190423263193 , 565.883970245137 , 324.075886340374 , 751.981021007397 , 365.020385784789 , 634.631608284800 , 870.681144389640 , 575.693922833089 , 86.586556686489 , 543.578672145469 , 673.891942208923 , 541.862760390298 , 944.641835403661 , 139.283899048892 , 246.761684842916 , 113.494460889474 , 638.090206007283 , 914.823583555335 , 206.606417462876 , 589.777735869628 , 502.260145197561 , 579.757814317903 , 544.030731986674 , 364.027470961226 , 606.539737608022 , 581.931435701986 , 10.679399664214 , 337.546152386227 , 529.854117598570 , 994.614151509414 , 665.253797196136 , 135.059930784168 , 539.887679072649 , 232.784895018668 , 148.506821836946 , 402.012584830847 , 141.217661420440 , 917.407115765932 , 521.877507187813 , 668.826020562516 , 66.326286676423 , 294.349504296278 , 840.675867407257 , 305.710788855281 , 831.241682220009 , 932.159845999022 , 36.987406399511 , 775.277101419523 , 563.714985498643 , 800.333762083496 , 611.743752768729 , 312.665814108569 , 171.978077350473 , 664.558791906972 , 563.892612333685 , 849.448967045284 , 758.225852407289 , 751.744185111613 , 748.869115683578 , 574.794067194860 , 829.798837222084 , 448.578010624459 , 673.592442791185 , 766.248302177991 , 152.662801609234 , 455.795426325605 , 990.010121375194 , 779.343244605189 , 361.782408668885 , 253.431809663337 , 35.709764297520 , 877.582622496309 , 769.672622600703 , 107.669947928365 , 541.994270236769 , 389.882316544079)
"p <- (913.809599612895, 195.184617797788 , 482.390524851619 , 355.440057159286 , 252.238876685380 , 415.562722721482 , 612.573889253824 , 841.812655335921 , 505.589444643811 , 678.461995018514 , 36.393315899922 , 625.297518341807 , 993.731457869481 , 41.589246154450 , 632.625389366601 , 593.152181516976 , 654.657766114507 , 470.710674033664 , 51.923553664518 , 2.327880375314 , 315.607696285911 , 860.229526244811 , 42.065476540857 , 848.669942849393 , 232.977560744436 , 691.114194622159 , 611.555808193656 , 871.141807772655 , 151.744513967412 , 636.167689501466 , 658.532815977176 , 590.621978600353 , 468.446995661718 , 770.847364589109 , 704.552743083740 , 515.772651155304 , 748.603529766676 , 320.051495646933 , 452.706289503140 , 128.212007947998 , 672.610701317176 , 16.369229849299 , 782.480016247550 , 263.366503906158 , 162.835299370387 , 923.580636753458 , 82.324452959251 , 121.483465493213 , 903.144559254483 , 647.080187498739 , 851.334835076406 , 544.299773886374 , 43.072371576313 , 664.077913867694 , 910.449141500167 , 964.560030330705 , 99.114379638628 , 198.372211824154 , 286.807618396517 , 801.876281558936 , 823.581458346424 , 163.851981253729 , 543.221956212087 , 282.045077397080 , 284.316912558798 , 598.551711360379 , 403.866336074384 , 51.051272034111 , 719.240330976492 , 672.711617954491 , 984.476891247987 , 708.164457590273 , 110.667051586940 , 456.262820744485 , 806.190423263193 , 565.883970245137 , 324.075886340374 , 751.981021007397 , 365.020385784789 , 634.631608284800 , 870.681144389640 , 575.693922833089 , 86.586556686489 , 543.578672145469 , 673.891942208923 , 541.862760390298 , 944.641835403661 , 139.283899048892 , 246.761684842916 , 113.494460889474 , 638.090206007283 , 914.823583555335 , 206.606417462876 , 589.777735869628 , 502.260145197561 , 579.757814317903 , 544.030731986674 , 364.027470961226 , 606.539737608022 , 581.931435701986 , 10.679399664214 , 337.546152386227 , 529.854117598570 , 994.614151509414 , 665.253797196136 , 135.059930784168 , 539.887679072649 , 232.784895018668 , 148.506821836946 , 402.012584830847 , 141.217661420440 , 917.407115765932 , 521.877507187813 , 668.826020562516 , 66.326286676423 , 294.349504296278 , 840.675867407257 , 305.710788855281 , 831.241682220009 , 932.159845999022 , 36.987406399511 , 775.277101419523 , 563.714985498643 , 800.333762083496 , 611.743752768729 , 312.665814108569 , 171.978077350473 , 664.558791906972 , 563.892612333685 , 849.448967045284 , 758.225852407289 , 751.744185111613 , 748.869115683578 , 574.794067194860 , 829.798837222084 , 448.578010624459 , 673.592442791185 , 766.248302177991 , 152.662801609234 , 455.795426325605 , 990.010121375194 , 779.343244605189 , 361.782408668885 , 253.431809663337 , 35.709764297520 , 877.582622496309 , 769.672622600703 , 107.669947928365 , 541.994270236769 , 389.882316544079)"
p
mean(p)
hist(p)
median(p)
mode(p)
var(p)
std(p)
sd(p) = sqrt(var(p))
sd(p) <- sqrt(var(p))
sqrt(var(p))
p <- (913.809599612895, 195.184617797788 , 482.390524851619 , 355.440057159286 , 252.238876685380 , 415.562722721482 , 612.573889253824 , 841.812655335921 , 505.589444643811 , 678.461995018514 , 36.393315899922 , 625.297518341807 , 993.731457869481 , 41.589246154450 , 632.625389366601 , 593.152181516976 , 654.657766114507 , 470.710674033664 , 51.923553664518 , 2.327880375314 , 315.607696285911 , 860.229526244811 , 42.065476540857 , 848.669942849393 , 232.977560744436 , 691.114194622159 , 611.555808193656 , 871.141807772655 , 151.744513967412 , 636.167689501466 , 658.532815977176 , 590.621978600353 , 468.446995661718 , 770.847364589109 , 704.552743083740 , 515.772651155304 , 748.603529766676 , 320.051495646933 , 452.706289503140 , 128.212007947998 , 672.610701317176 , 16.369229849299 , 782.480016247550 , 263.366503906158 , 162.835299370387 , 923.580636753458 , 82.324452959251 , 121.483465493213 , 903.144559254483 , 647.080187498739 , 851.334835076406 , 544.299773886374 , 43.072371576313 , 664.077913867694 , 910.449141500167 , 964.560030330705 , 99.114379638628 , 198.372211824154 , 286.807618396517 , 801.876281558936 , 823.581458346424 , 163.851981253729 , 543.221956212087 , 282.045077397080 , 284.316912558798 , 598.551711360379 , 403.866336074384 , 51.051272034111 , 719.240330976492 , 672.711617954491 , 984.476891247987 , 708.164457590273 , 110.667051586940 , 456.262820744485 , 806.190423263193 , 565.883970245137 , 324.075886340374 , 751.981021007397 , 365.020385784789 , 634.631608284800 , 870.681144389640 , 575.693922833089 , 86.586556686489 , 543.578672145469 , 673.891942208923 , 541.862760390298 , 944.641835403661 , 139.283899048892 , 246.761684842916 , 113.494460889474 , 638.090206007283 , 914.823583555335 , 206.606417462876 , 589.777735869628 , 502.260145197561 , 579.757814317903 , 544.030731986674 , 364.027470961226 , 606.539737608022 , 581.931435701986 , 10.679399664214 , 337.546152386227 , 529.854117598570 , 994.614151509414 , 665.253797196136 , 135.059930784168 , 539.887679072649 , 232.784895018668 , 148.506821836946 , 402.012584830847 , 141.217661420440 , 917.407115765932 , 521.877507187813 , 668.826020562516 , 66.326286676423 , 294.349504296278 , 840.675867407257 , 305.710788855281 , 831.241682220009 , 932.159845999022 , 36.987406399511 , 775.277101419523 , 563.714985498643 , 800.333762083496 , 611.743752768729 , 312.665814108569 , 171.978077350473 , 664.558791906972 , 563.892612333685 , 849.448967045284 , 758.225852407289 , 751.744185111613 , 748.869115683578 , 574.794067194860 , 829.798837222084 , 448.578010624459 , 673.592442791185 , 766.248302177991 , 152.662801609234 , 455.795426325605 , 990.010121375194 , 779.343244605189 , 361.782408668885 , 253.431809663337 , 35.709764297520 , 877.582622496309 , 769.672622600703 , 107.669947928365 , 541.994270236769 , 389.882316544079)
mean(p)
hist(p)
median(p)
var(p)
sqrt(var(p))
sd(p)
range(p)
max(p) - min(p)
summary(p)
fivenum(ldeaths)
quantile(p)
boxplot.stats(p)
IQR(p)