diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d44df33 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/DATA-SCIENCE-AN-OVERVIEW-IN-R.docx b/DATA-SCIENCE-AN-OVERVIEW-IN-R.docx new file mode 100644 index 0000000..d417763 Binary files /dev/null and b/DATA-SCIENCE-AN-OVERVIEW-IN-R.docx differ diff --git a/INTRODUCTION-TO-PROBABILITY-USING-R.docx b/INTRODUCTION-TO-PROBABILITY-USING-R.docx new file mode 100644 index 0000000..3b90b19 Binary files /dev/null and b/INTRODUCTION-TO-PROBABILITY-USING-R.docx differ diff --git a/INTRODUCTION-TO-R-FOR-DATA-SCIENCE-PART-I.docx b/INTRODUCTION-TO-R-FOR-DATA-SCIENCE-PART-I.docx new file mode 100644 index 0000000..9494294 Binary files /dev/null and b/INTRODUCTION-TO-R-FOR-DATA-SCIENCE-PART-I.docx differ diff --git a/Introduction-to-probability-using-r.Rmd b/Introduction-to-probability-using-r.Rmd new file mode 100644 index 0000000..cfbf0ac --- /dev/null +++ b/Introduction-to-probability-using-r.Rmd @@ -0,0 +1,368 @@ +--- +title: "INTRODUCTION TO PROBABILITY USING R" +output: + word_document: default + html_document: + df_print: paged +--- +BEING A SHORT TRAINING PRESENTED AT OFFA R USERS GROUP MEETING ON 13TH FEBRUARY, 2024 AT STATISTICAL LABORATORY, STATISTICS DEPARTMENT, THE FEDERAL POLYTECHNIC OFFA, NIGERIA +BY +UDOKANG, ANIETIE EDEM (OGANIZER, ORUG) +CHIF LECTURER, STATISTICS DEPARTMENT,THE FEDERAL POLYTECHNIC OFFA, NIGERIA + +#Introduction +This topic is carefully chosen because of the important of probability in Statistics, most especially when it has to do with inferential Statistics. +We deal with probability in many fields of Statistics such as Econometrics, Time Series, Biostatistics, Medical Statistics, Sampling Theory, Inference and Business Statistics. +Welcome on board as we discuss the elementary part of Statistics that has to do with the Concepts of Probability. +Before then, let’s get ourselves familiar with R Software. +#What is R? +R is a free statistical software created by statisticians Ross Ihaka and Robert Gentleman and supported by R Core Team and the R Foundation for Statistical Computing. +#Where can I Find R? +Visit: https://cran.r-project.org/ +Follow the instructions for free download and install in your system. +Display of the R Console (A Tool to type command and see the result of the command) + +#What is Probability? +Probability can be defined as a quantity from 0 to 1used to measure the likelihood or the chance of an event occurring. +#Two Approaches of Probability +*Classical Approach -This probability approach assumes equally likely outcomes based on prior knowledge. + +*Relative Frequency – This approach is based on observed occurrences over a large number of trials. +Formula +P(A) = n(A)/n(S) +Or +P(A) = f/n +Where +P(A) = number of event A +n(S) = number all possible events = sample space +f = frequency of a sub group +n = Total frequency of all the groups = sample size + +##llustrative Examples (R-4.4.0) +#Example 1 (Classical Approach) +An unbiased coin has two sides Head (H) and Tail (T). This implies that each side has an equal chance of occurrence when toss or flip. What is the probability of having a head when the coin is tossed once? + +```{r} +outcomes <- c("Head", "Tail") +total_outcomes <- length(outcomes) +total_outcomes +``` +```{r} +head.outcome <- length(outcomes[outcomes == "Head"]) +head.outcome +``` +```{r} +prob.head <- head.outcome / total_outcomes +prob.head +``` +#Example 2 (Classical Approach) +Consider a situation that an unbiased coin is tossed twice or two of the coins are tossed twice. The sample space is HH, HT, TH and TT. +What is the probability of having two heads? +```{r} +outcomes <- c("HH", "HT", "TH", "TT") +total_outcomes <- length(outcomes) +total_outcomes +``` +```{r} +head.outcomes <- length(outcomes[outcomes == "HH"]) +head.outcomes +``` +```{r} +prob.head <- head.outcomes / total_outcomes +prob.head +``` +#Example 3 (Relative Frequency Approach) +The scores and grades students in an examination are 75 (A), 75 (A), 70 (AB), 70 (AB), 70 (AB), 70 (AB), 70(AB), 66(B), 66 (B) and 66(B). +What is the probability of a student having AB in the examination? +There are ten students in the sample space, out which three have AB. +```{r} +grades <- c("A", "A", "AB", "AB","AB","AB","AB","B","B","B") +total.grades <- length(grades) +total.grades +``` +```{r} +AB.grades <- length(grades[grades == "AB"]) +AB.grades +``` +```{r} +prob.AB <- AB.grades / total.grades +prob.AB +``` +We can as well put up a frequency distribution table and get the probabilities of 75 (A), AB (70) and B (66). +```{r} +Score<-c(75,75,70,70,70,70,70,66,66,66) +factor(Score) +table(Score) +prop.table(table(Score)) +``` +#Probability Properties with Illustrations and Examples +1. 0% replace_na(list(x = "x2", f = 4)) +``` + +#Filling missing values up with the preceding values +```{r} +xf<-tibble(x=c("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10"),f=c(1,4,2, NA,NA,6,7,9,NA,NA),) +xf +xf1 <- xf %>% fill(f, .direction = 'up') +xf1 +``` + +#Filling missing values down with the preceding values +```{r} +xf2 <- xf1 %>% fill(f, .direction = 'down') +xf2 +``` +#Splitting columns using “separate” function +```{r} +GP<- tibble(id = 1:2, x = c("m-360", "f-580")) +GP +``` + +#GP is the data frame (df) representing population by gender +```{r} +df %>% separate(x, c("gender", "unit")) +``` +#Splitting columns using “strsplit” function +```{r} +Students<- data.frame(Programme = c("ND Statistics", "HND Computer", "NID Welding", "BTECH Civil"), Pop = c(120, 200, 180, 78) ) +Students +``` + +```{r} +Students[c("Programme","Department")]<- do.call(rbind, strsplit(Students$Programme, " ")) +print(Students) +``` + +#Uniting columns using “unite” function +```{r} +x<-c("B","P","S","A","T","O","J","C","BT","BR","K") +y<-c("Ben","Philip","Segun","Ani","Tom","Okon","Jide","Chidi","Betty","Bros","Ken") +df<-data.frame(x,y) +df +``` + +```{r} + z<-data.frame(x=c("B","P","S","A","T","O","J","C","BT","BR","K"),y=c("Ben","Philip","Segun","Ani","Tom","Okon","Jide","Chidi","Betty","Bros","Ken")) +df %>% unite("z", x:y, remove = FALSE) +``` +```{r} +df %>% unite("z", x:y, remove = TRUE) +``` + +#We appreciate your patient and interest in participating in the discussion today. The Offa-R-Users-Group (ORUG) is a place to learn and share knowledge in the use of R. I wish to see you next time. If you are a guest, find time to register as a member to actualize your goal in using R. +The ORUG (https://www.meetup.com/fedpofa-r-users-group/ ) is sponsored by R Consortium and AniKem_Consult. For any Enquiry Contact the Organizer (WhatsApp: +2349030912602, email: anietieeu@yahoo.com) + +#THANK YOU FOR PARTICIPATING IN THIS EVENT + + + + + diff --git a/LICENSE b/LICENSE index 0e259d4..354f1e0 100644 --- a/LICENSE +++ b/LICENSE @@ -1,121 +1,121 @@ -Creative Commons Legal Code - -CC0 1.0 Universal - - CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE - LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN - ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS - INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES - REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS - PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM - THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED - HEREUNDER. - -Statement of Purpose - -The laws of most jurisdictions throughout the world automatically confer -exclusive Copyright and Related Rights (defined below) upon the creator -and subsequent owner(s) (each and all, an "owner") of an original work of -authorship and/or a database (each, a "Work"). - -Certain owners wish to permanently relinquish those rights to a Work for -the purpose of contributing to a commons of creative, cultural and -scientific works ("Commons") that the public can reliably and without fear -of later claims of infringement build upon, modify, incorporate in other -works, reuse and redistribute as freely as possible in any form whatsoever -and for any purposes, including without limitation commercial purposes. -These owners may contribute to the Commons to promote the ideal of a free -culture and the further production of creative, cultural and scientific -works, or to gain reputation or greater distribution for their Work in -part through the use and efforts of others. - -For these and/or other purposes and motivations, and without any -expectation of additional consideration or compensation, the person -associating CC0 with a Work (the "Affirmer"), to the extent that he or she -is an owner of Copyright and Related Rights in the Work, voluntarily -elects to apply CC0 to the Work and publicly distribute the Work under its -terms, with knowledge of his or her Copyright and Related Rights in the -Work and the meaning and intended legal effect of CC0 on those rights. - -1. Copyright and Related Rights. A Work made available under CC0 may be -protected by copyright and related or neighboring rights ("Copyright and -Related Rights"). Copyright and Related Rights include, but are not -limited to, the following: - - i. the right to reproduce, adapt, distribute, perform, display, - communicate, and translate a Work; - ii. moral rights retained by the original author(s) and/or performer(s); -iii. publicity and privacy rights pertaining to a person's image or - likeness depicted in a Work; - iv. rights protecting against unfair competition in regards to a Work, - subject to the limitations in paragraph 4(a), below; - v. rights protecting the extraction, dissemination, use and reuse of data - in a Work; - vi. database rights (such as those arising under Directive 96/9/EC of the - European Parliament and of the Council of 11 March 1996 on the legal - protection of databases, and under any national implementation - thereof, including any amended or successor version of such - directive); and -vii. other similar, equivalent or corresponding rights throughout the - world based on applicable law or treaty, and any national - implementations thereof. - -2. Waiver. To the greatest extent permitted by, but not in contravention -of, applicable law, Affirmer hereby overtly, fully, permanently, -irrevocably and unconditionally waives, abandons, and surrenders all of -Affirmer's Copyright and Related Rights and associated claims and causes -of action, whether now known or unknown (including existing as well as -future claims and causes of action), in the Work (i) in all territories -worldwide, (ii) for the maximum duration provided by applicable law or -treaty (including future time extensions), (iii) in any current or future -medium and for any number of copies, and (iv) for any purpose whatsoever, -including without limitation commercial, advertising or promotional -purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each -member of the public at large and to the detriment of Affirmer's heirs and -successors, fully intending that such Waiver shall not be subject to -revocation, rescission, cancellation, termination, or any other legal or -equitable action to disrupt the quiet enjoyment of the Work by the public -as contemplated by Affirmer's express Statement of Purpose. - -3. Public License Fallback. Should any part of the Waiver for any reason -be judged legally invalid or ineffective under applicable law, then the -Waiver shall be preserved to the maximum extent permitted taking into -account Affirmer's express Statement of Purpose. In addition, to the -extent the Waiver is so judged Affirmer hereby grants to each affected -person a royalty-free, non transferable, non sublicensable, non exclusive, -irrevocable and unconditional license to exercise Affirmer's Copyright and -Related Rights in the Work (i) in all territories worldwide, (ii) for the -maximum duration provided by applicable law or treaty (including future -time extensions), (iii) in any current or future medium and for any number -of copies, and (iv) for any purpose whatsoever, including without -limitation commercial, advertising or promotional purposes (the -"License"). The License shall be deemed effective as of the date CC0 was -applied by Affirmer to the Work. Should any part of the License for any -reason be judged legally invalid or ineffective under applicable law, such -partial invalidity or ineffectiveness shall not invalidate the remainder -of the License, and in such case Affirmer hereby affirms that he or she -will not (i) exercise any of his or her remaining Copyright and Related -Rights in the Work or (ii) assert any associated claims and causes of -action with respect to the Work, in either case contrary to Affirmer's -express Statement of Purpose. - -4. Limitations and Disclaimers. - - a. No trademark or patent rights held by Affirmer are waived, abandoned, - surrendered, licensed or otherwise affected by this document. - b. Affirmer offers the Work as-is and makes no representations or - warranties of any kind concerning the Work, express, implied, - statutory or otherwise, including without limitation warranties of - title, merchantability, fitness for a particular purpose, non - infringement, or the absence of latent or other defects, accuracy, or - the present or absence of errors, whether or not discoverable, all to - the greatest extent permissible under applicable law. - c. Affirmer disclaims responsibility for clearing rights of other persons - that may apply to the Work or any use thereof, including without - limitation any person's Copyright and Related Rights in the Work. - Further, Affirmer disclaims responsibility for obtaining any necessary - consents, permissions or other rights required for any use of the - Work. - d. Affirmer understands and acknowledges that Creative Commons is not a - party to this document and has no duty or obligation with respect to - this CC0 or use of the Work. +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/Parameter-Estimation-in-Non-Linear-Regression-Model-Using-r.Rmd b/Parameter-Estimation-in-Non-Linear-Regression-Model-Using-r.Rmd new file mode 100644 index 0000000..a257fb3 --- /dev/null +++ b/Parameter-Estimation-in-Non-Linear-Regression-Model-Using-r.Rmd @@ -0,0 +1,245 @@ +--- +title: "Parameter Estimation in Non-Linear Regression Model Using R" +output: html_notebook +--- + +Being a Short Training Material Presented at the Offa-R-Users-Group (ORUG) on 30th May, 2024. +By +Udokang, Anietie Edem +Organizer + +#Introduction# +The previous discussion on regression analysis in this group was based on linear relationship, where the parameters and explanatory variable(s) were both linear. Today we want to consider other situations whereby the parameters or explanatory or both, are not linear referred to as non-linear relationship which can as well be explained by a non-linear model. +It should be noted that for the parameters of a non-linear regression model to be estimated, the said model can to be transformed to a linear model using various transformation methods suitable to the model. Therefore, non-linear models will be presented in terms of linearity in parameters and explanatory variables and use it to compute the parameters of the transformed model which will be used to get the estimate of the initial parameters. +#1. Polynomial Non-Linear Regression Model# +This type of regression model can be described as non-linear regression model because its non-linearity in variables, though its parameters may be linear. The polynomial non-linear regression model may not require the model to be transformed to linear model but action should be taken on variables with the power greater than 1.There are two types of polynomial regression models which are polynomial in one variable and polynomial in k variables. +##i. Polynomial in One Variable## +Polynomial regression model with only one independent variables with different powers is polynomial regression model in one variable. +#The model:# +#The 2nd polynomial (quadratic) regression model in one variable +$y=a_{0}+a_{1}x+a_{2}x^{2}+e$ + +#The 3rd polynomial (cubic) regression model in one variable +$y=a_{0}+a_{1}x+a_{2}x^{2}+a_{3}x^{3}+e$ + +#The 4th polynomial (quatic) regression model in one variable +$y=a_{0}+a_{1}x+a_{2}x^{2}+a_{3}x^{3}+a_{4}x^{4}+e$ + +#The nth polynomial regression model in one variable +$y=a_{0}+a_{1}x+a_{2}x^{2}+a_{3}x^{3}+a_{4}x^{4}+\dots+a_{n}x^{n}+e$ + +The estimation of the parameters of the nth polynomial regression model takes the process of least square estimation of the linear regression. +The illustration will be done on quadratic and cubic polynomial regression which the same procedure will be followed to kth polynomial. + + +```{r} +#Illustrative Example +#Quadratic (2nd) Polynomial Regression +x<-c(25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40) +y<-c(65,63,62,61,60,57,56,55,56,58,60,62,63,64,65,66) +plot(x, y) +``` + + +```{r} +df <- data.frame(x, y) +m1= lm(y ~ x+I(x^2)) +m1 +``` + + +```{r} +summary(m1) +``` + + +```{r} +predict(m1) +``` + +#Plot of the actual values and the regression line +```{r} +library(ggplot2) +ggplot(df, aes(x, y)) +geom_point() +geom_line(aes(x, predict(m1)),col="red") + +ggtitle("Quadratic Polynomial Regression") +``` + +#Illustrative Example +#Cubic (3rd) Polynomial Regression +```{r} +x<-c(6,9,12,16,22,28,33,40,47,51,55,60) +y<-c(14,28,50,64,67,57,55,57,68,74,88,110) +plot(x, y) +``` + +```{r} +df <- data.frame(x, y) +m2= lm(y ~ x+I(x^2)+ I(x^3)) +m2 +``` + +```{r} +summary(m2) +``` + +```{r} +predict(m2) +``` + +```{r} +library(ggplot2) +ggplot(df, aes(x, y))+geom_point() +geom_line(aes(x, predict(m2)),col="blue") +ggtitle("Cubic Polynomial Regression") +``` + +##ii. Polynomial Regression Model in K Variables +The polynomial regression with number of explanatory variables more than 0ne ( ) is said to Polynomial Regression Model in K Variables. +#The polynomial regression model is + +#The 2nd polynomial (quadratic) regression model in two variables +$y=a_{0}+a_{1}x+a_{2}z+a_{3}x^{2}+a_{4}z^{2}+e$ + +#The 3rd polynomial (cubic) regression model in one variable +$y=a_{0}+a_{1}x+a_{2}z+a_{3}x^{2}+a_{4}z^{2}+a_{5}x^{3}+a_{6}z^{3}+e$ + +#The 4th polynomial (quatic) regression model in one variable +$y=a_{0}+a_{1}x+a_{2}z+a_{3}x^{2}+a_{4}z^{2}+a_{5}x^{3}+a_{6}z^{3}+a_{7}x^{4}+a_{8}z^{4}+e$ + +#The nth polynomial regression model in one variable +$y=a_{0}+a_{1}x+a_{2}z+a_{3}x^{2}+a_{4}z^{2}+a_{5}x^{3}+a_{6}z^{3}+\dots+a_{n-1}x^{n-1}+a_{n}z^{n}+e$ + +The estimation of the model parameters is done just like the linear in variable case, where least square method of estimation is used. +We will consider the quadratic case and the idea can be applied to cubic. +#Illustrative Example +#Quadratic (2nd) Polynomial Regression + + +```{r} +x<-c(25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40) +y<-c(65,63,62,61,60,57,56,55,56,58,60,62,63,64,65,66) +z<-c(23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38) +plot(x, y) +``` + +```{r} +plot(z, y) +``` + +```{r} +df <- data.frame(x, y,z) +m3= lm(y ~ x+I(x^2)+ z+I(z^2)) +m3 +``` + +```{r} +summary(m3) +``` + +```{r} +predict(m3) +``` + +#Plot of regression line with x +```{r} +library(ggplot2) +ggplot(df, aes(x, y)) +geom_point() +geom_line(aes(x, predict(m3)),col="red") + +ggtitle("Quadratic Polynomial Regression") +``` + +#Plot regression line with z +```{r} +#Plot regression line with z +ggplot(df, aes(z, y)) +geom_point() +geom_line(aes(z, predict(m3)),col="red") + +ggtitle("Quadratic Polynomial Regression") +``` + +##2. Exponential Non-Linear Regression Model with Natural Logarithm Transformation +This type of non-linear model can be transformed with with logarithm to base e to make it a linear model. +An example of such model is $y=ae^{bx}$ which can be transformed to linear model taking the natural log of both sides of the equation. +$log_{e}y=log_{e}a+bx$ +#let +$log_{e}y=y^*$ and $log_{e}a=b_{0}$ +#Therefore, the estimate of the response variable from the new model gives the regression line equation as +$y^*=b_{0}+bx$ +from $log_{e}a=b_{0}$ +Therefore, +$a=e^{b_{0}}$ +Then we can now estimate $y=ae^{bx}$ +```{r} +#Illustrative Example +x <- c(2,4,6,8,10,12,14,16,18) +y <- c(5,7,8,9,10,12,14,16,20) +df <- data.frame(x, y) +plot(x, y) +``` + +```{r} +m4= lm(log(y) ~ x) +m4 +``` + +```{r} +b0= 1.38460 #The constant term of the transformed regression model (b0 = loga ) +a=exp(b0) #The constant term of exponential regression model +a +``` + +```{r} +b= 0.09282 #The coefficient of the transformed model = the coefficient of the exponential model +predicty=a*exp(b*x) #To predict the exponential regression model +predicty +``` + +#Plot of regression line with actual values +```{r} +ggplot(df, aes(x, y)) +geom_point() +geom_line(aes(x, predicty),col="red") + +ggtitle("Exponential Regression") +``` +#This an example of exponential growth where b>1 (if 05, there is high correlation between a given explanatory variable and other explanatory variables, hence existence of multicollinearity +```{r} +library(car) +vif(lm1) +``` + +#There is existence of multicollinearity in IMP and EXP at severe level. But GVTREV has a moderate existence of autocorrelation which may require any action. +*Action:* Remove EXP with the highest VIF or find an appropriate way of combining the two of EXP and IMP. + +#5. Residuals must be Normally Distributed +##Q-Q Plot +```{r} +library(forecast) +resid<- resid(lm1) +qqnorm(resid) +``` + + +#This is near normality even though the points are not in a straight line but are close it, except one point which normality can be considered by these observation. + +#Let this be sunjected to a test of hypothesis using Shapiro-Wilk Test. + + ##Shapiro-Wilk Test. + +$H_{0}$: The residuals are normally distributed +Vs +$H_{1}$: The residuals are not normally distributed +```{r} +shapiro.test(residuals(lm1)) +``` + +# The Shapiro test statistic W = 0.95848, p-value = 0.5139>0.05, hence the residuals have a normal distribution. +*Action:* No action. +If the residuals are not normally distributed the original can be transformed or another model of non-linear form can be used. + +#This is end of today’s short training hosted by the Offa-R-Users-Group (ORUG) a place for learning and using R. I wish to you to be part of the training session either online or physical. If you are a guest, find time to register as a member to actualize your goal in using R. +The ORUG (https://www.meetup.com/fedpofa-r-users-group/ ) is sponsored by R Consortium and AniKem_Consult. For any Enquiry Contact the Organizer (WhatsApp: +2349030912602, email: anietieeu@yahoo.com) + +#BY NEXT QUARTER WE SHALL CONSIDER ACTIONS TO BE TAKEN WHEN THE ASSUMPTIONS ARE VIOLATED. + +THANKS FOR BEING PART OF THIS SHORT TRAINING. + + + diff --git a/Test-for-the-Assumptions-of-Linear-Regression-Using-R.docx b/Test-for-the-Assumptions-of-Linear-Regression-Using-R.docx new file mode 100644 index 0000000..f1c3aca Binary files /dev/null and b/Test-for-the-Assumptions-of-Linear-Regression-Using-R.docx differ diff --git a/Training in R.zip b/Training in R.zip new file mode 100644 index 0000000..93b2935 Binary files /dev/null and b/Training in R.zip differ diff --git a/rugs-materials.Rproj b/rugs-materials.Rproj new file mode 100644 index 0000000..3af27f6 --- /dev/null +++ b/rugs-materials.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX