Skip to main content

Linear Regression in R

        Linear regression is a statistical method used to model the linear relationship between a dependent variable and one or more independent variables. In this example, the dependent variable is y and the independent variable is x. The goal of the model is to find the line of best fit that describes the relationship between x and y in the data.

 

        First example of linear regression application in R :


First, we need to load the necessary libraries:


library(tidyverse)
library(broom)


Next, let's read in the data and take a look at it:

 

data <- read_csv("data.csv")

head(data)
#   x          y
# 1 1  2.5164708
# 2 2  3.4593287
# 3 3  4.7047409
# 4 4  5.4292901
# 5 5  6.8230604
# 6 6  7.8731561
 

 

Now, let's fit the linear regression model:

 

model <- lm(y ~ x, data=data)
 

 

We can then use the summary function to get a summary of the model fit:

 

summary(model)

# Call:
# lm(formula = y ~ x, data = data)
#
# Residuals:
#      Min       1Q   Median       3Q      Max
# -0.92588 -0.33667  0.01591  0.33919  1.10721
#
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)    
# (Intercept) 1.237039   0.257350   4.800 0.000145 ***
# x           0.937292   0.029744  31.446  < 2e-16 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
# Residual standard error: 0.5013 on 8 degrees of freedom
# Multiple R-squared:  0.9992,    Adjusted R-squared:  0.9991
# F-statistic: 989.6 on 1 and 8 DF,  p-value: < 2.2e-16
 

 

From the summary, we can see that the model has a high R-squared value, indicating a good fit. We can also see the coefficients for the model, including the intercept and the slope for the "x" variable.

Finally, we can use the model to make predictions:

 

predictions <- predict(model, data.frame(x=c(6, 7, 8)))
predictions
#       1        2        3
# 7.87316 8.81045 9.74774

 

 

 

        Another example like the below code will generate some fake data, fit a linear regression model to it, make predictions using the model, and visualize the fit.


# First, we'll start by installing and loading the packages we need
install.packages("tidyverse")
install.packages("broom")

library(tidyverse)
library(broom)

# Next, let's generate some fake data for our example
set.seed(123)
fake_data <- tibble(
  x = rnorm(100),
  y = 2 * x + rnorm(100)
)

# Now we can fit a linear model to our data
model <- lm(y ~ x, data = fake_data)

# We can use the broom package to tidy up the model output
tidy(model)

# And we can make predictions using our model
fake_data %>%
  mutate(prediction = predict(model, new_data = .))

# We can also plot the model to visualize the fit
ggplot(fake_data, aes(x = x, y = y)) +
  geom_point() +
  geom_line(aes(y = prediction), color = "red")



Popular posts from this blog

G*Power Perisian Bagi Pengiraan Sampel Saiz.

Penggunaan Gpower kerap menekan kepada tiga langkah berikut: Memilih ujian statistik yang sesuai atau padan dengan masalah yang di kaji. Memilih di antara lima jenis analisis kuasa (power analysis) yang di sediakan. Sediakan parameter input yang di perlukan analasis dan klik pada “calculate” Pada Langkah 1, pendekatan yang digunakan untuk memilih ujian statistic (statistical test) adalah melalui dua pendekatan, iaitu distribution based atau design-based approach . Distribution-based approach to the test selection Melalui pendekatan distribution-based , pendekatan pertama adalah melihat pada kumpulan umum ujian statistik menggunakan '' Test family'' menu yang terdapat di window atau tingkap utama.   Ujian statitik ( Statistical test ) menu akan berubah mengikut pilihan di dalam '' Test family'' . Ujian-ujian yang ada akan selaras mengikut pada '' test family'' yang di pilih sahaja. Design-based approach to the test se...

Krejcie & Morgan sample size calculator

Krejcie & Morgan Sample Size Calculator Enter Population Size (N): Calculate Sample Size Recommended Sample Size (n): 📘 About This Calculator This calculator uses the Krejcie & Morgan (1970) formula to estimate the minimum sample size required when the total population size is known. It is commonly used in social sciences, education, and health research. The formula is: n = (X² × N × P × (1 − P)) / (d² × (N − 1) + X² × P × (1 − P)) X² = 3.841 (for 95% confidence level) P = 0.5 (maximum variability) d = 0.05 (±5% precision) 📚 Citation Krejcie, R.V., & Morgan, D.W. (1970). Determining Sample Size for Research Activities . Educational and Psychological Measurement, 30 (3), 607–610. https://doi.org/10.1177/001316447003000308

ANOVA vs MANOVA

Perbezaan utama di antara ANOVA dan MANOVA adalah jumlah bilangan pembolehubah bersandar ( dependent variable ). Walaupun begitu, jika terdapat pembolehubah bersandar lebih daripada satu, masih bukan masalah dan merupakan pilihan lain jika pengkaji mahu untuk menguji secara berasingan menggunakan analisa ANOVA bagi setiap pembolehubah bersandar itu. Jadi, kenapa perlu menggunakan pengiraan MANOVA ini berbanding beberapa analisa menggunakan ANOVA jika terdapat pembolehubah bersandar yang lebih daripada satu seperti dua, tiga atau empat pembolehubah bersandar? Terdapat dua sebab utama, MANOVA berpotensi menggantikan ANOVA dalam beberapa keadaan. Pertama melibatkan teori yang logik dan kedua melibatkan statistiknya. Sebagai contoh: Skormatematik + skorfizik + skorkimia sebagai fungsi kepada ( as a function of ) minuman tambahan ( tiga tahap / level ) Minuman tambahan, pembolehubah tidak bersandar atau bebas dengan tiga tahap ( levels ) : minuman kurma, minuman madu dan minuma...