################## 1. example ################################ # Create a user-defined function to convert temperature from degrees Fahrenheit to degrees Celsius # (the result will be presented as “….degrees Fahrenheit = ….. degrees Celsius”. ) # T(°C) = (T(°F) - 32)× 5/9 FtoC <- function(f) { c <- (f - 32 ) * 5 / 9 c=round(c,1) cat(f,"Fahrenheit degrees =", c ,"Celsius degrees") } FtoC(120) ####################### 2. example ########################### # Create a user-defined function to convert temperature from degrees Fahrenheit to degrees Celsius # and vice versa. Function allows you to decide what type of conversion (F to C or C to F) will be # used and the result will be presented automatically as "… .degrees Fahrenheit =… .. degrees # Celsius " or"… .degrees (Celsius) =… .. degrees (Fahrenheit) " according to the selected # conversion type. TempConv <- function(t,FtoC=T) { if(FtoC) { c <- (t - 32 ) * 5 / 9 c=round(c,1) cat(t,"Fahrenheit degrees =", c ,"Celsius degrees") } else { f <- (9*t/5)+32 cat(t,"Celsius degrees =", f ,"Fahrenheit degrees") } } TempConv(120) TempConv(25,FtoC=F) ####################### 3. example ################################# # Create a user defined function to calculate the statistical measure of central tendency (mean or # median) and variability (standard deviation or interquartile range - IQR) according to the result # of the test of normality. The function allows you to specify α value for the test. The result will # present the p-value of the normality test and conclusion that data are sampled from normal or # non-normal population, respectively, and automatically presents the mean and standard deviation # for data from a population with a normal distribution, while for data from a population with a # non-normal distribution the median and IQR. mysummary = function(x,alpha=0.05) { x=x[!is.na(x)] SW=shapiro.test(x) if (SW$p.value>alpha) { center = mean(x); spread = sd(x) cat("p-value = ",SW$p.value, "data are sampled from normal distribution","\n","Mean = ", center, "\n","SD = ",spread, "\n") } else { center = median(x); spread = IQR(x) cat("p-value = ",SW$p.value, "data are not sampled from normal distribution","\n","Median = ", center, "\n","IQR = ", spread, "\n") } } a=c(2,5,8,2,4,6,4,7,8,9,5,4,2,5,7,5,6) b=c(2,5,8,2,4,6,4,7,8,9,5,4,2,5,7,55,68) mysummary(a) mysummary(b) sd(b) mean(b) ############################ 4. example #################################### # Create a user defined function to calculate the confidence interval of standard deviation for # small sample. Function allows you to choose one sided or two sided test and specify α value. The # result will present the lower and upper limits of the interval (in the same way as in the example # of the confidence interval of mean) ci_sd<-function(sample, alpha=0.05, one_sided=FALSE) { sample<-sample[!is.na(sample)] sample_var<-var(sample) n<-length(sample) low<- ifelse(one_sided, sqrt((n-1)*sample_var/qchisq(1-alpha, n-1)), sqrt((n-1)*sample_var/qchisq(1-alpha/2, n-1)) ) upp<-ifelse(one_sided, sqrt((n-1)*sample_var/qchisq(alpha, n-1)), sqrt((n-1)*sample_var/qchisq(alpha/2, n-1) )) c(low,upp) } sd_a2= ci_sd(a) sd_a2 sd_a1= ci_sd(a,one_sided=T) sd_a1 sd_a2= ci_sd(a) sd_a2 sd_a1= ci_sd(a,one_sided=T) sd_a1 #function "confint.sd" - library "Ecfun" sd1=confint.sd(sd(a),length(a)-1) sd1 sd2=confint.sd(sd(a),length(a)-1, level=0.90) sd2