This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
create a function to compute correlation coefficient
correlation_coff <- function(x,y){
xy <- x * y
xy
#sum of xy
sum_xy<-sum(xy)
sum_xy
# n*(sum of xy)
n<-length(x)
n
n*(sum_xy)
# sum x
sum_x<-sum(x)
sum_x
# sum y
sum_y<-sum(y)
sum_y
# sum of x * sum of y
sum_x*sum_y
#numerator
num<-n*(sum_xy)-(sum_x)*(sum_y)
num
# sq of x
x_sq<-x^2
x_sq
# sum of sq of x
sum_x_sq<-sum(x_sq)
sum_x_sq
# n*sum of sq of x
n*sum_x_sq
#sq of sum x
sq_sum_x<-(sum_x)^2
sq_sum_x
#
(n*sum_x_sq)-(sq_sum_x)
# sq of y
y_sq<-y^2
y_sq
# sum of sq of y
sum_y_sq<-sum(y_sq)
sum_y_sq
#n*sum of sq of y
n*sum_y_sq
# sq of sum y
sq_sum_y<-(sum_y)^2
sq_sum_y
#
(n*sum_y_sq)-(sq_sum_y)
# denominator
deno<-((n*sum_x_sq)-(sq_sum_x))*((n*sum_y_sq)-(sq_sum_y))
deno
# sq root of denominator
sq_root_deno<-deno^0.5
sq_root_deno
# numerator / denominator(R)
r<-num/sq_root_deno
r
return(r)
}
x <- c(1, 2, 3, 4, 5)
y <- c(2, 3, 4, 5, 6)
correlation_coff(x,y)
## [1] 1
# Function to add any number of values
add_values <- function(...) {
# Use the sum() function to add all the values
result <- sum(...)
return(result)
}
add_values(1,2,4)
## [1] 7
add_values(0,5,6,7,8,3,45.5)
## [1] 74.5