# rmarkdown::run("fits.Rmd") library(learnr) library(tutor) library(tidyverse) gradethis::gradethis_setup()
Let's say we have these data in a tibble called df
and want to fit them with a linear model:
x <- 1:10 y <- .5*x + runif(length(x))*.8 df <- tibble(x,y) ggplot(df, aes(x = x, y = y)) + expand_limits(x=0,y=0)+ geom_point()
Let's fit it with free parameters:
head(df,2) fit <- lm(data = ___, ___ ~ ___) # Print the fit summary summary(___) # Let's plot the result df %>% ggplot(aes(x = x, y = y)) + expand_limits(x = 0, y = 0) + geom_point()+ geom_line(aes(y = predict(fit)), col="red")
head(df, 2) fit <- lm(data = df, y ~ x) # Print the fit summary summary(fit) # Let's plot the result df %>% ggplot(aes(x = x, y = y)) + expand_limits(x = 0, y = 0) + geom_point() + geom_line(aes(y = predict(fit)), col = "red")
grade_code()
Now let's say we want to fix the intercept in 0 or in 1, how would you do it?
fit_fixed0 <- lm(data = ___, ___ ~ ___) fit_fixed1 <- lm(data = ___, ___ ~ ___) # Let's plot the result and compare it to the previous one df %>% ggplot(aes(x = x, y = y)) + expand_limits(x = 0, y = 0) + geom_point() + geom_line(aes(y = predict(fit), col = "Free")) + geom_line(aes(y = predict(fit_fixed0), col = "Fixed 0")) + geom_line(aes(y = 1 + coef(fit_fixed1) * x, col = "Fixed 1"))
fit_fixed0 <- lm(data=df, y ~ x+0) fit_fixed1 <- lm(data=df, y-1 ~ x+0) # Let's plot the result and compare it to the previous one df %>% ggplot(aes(x = x, y = y)) + expand_limits(x = 0, y = 0) + geom_point() + geom_line(aes(y = predict(fit), col = "Free")) + geom_line(aes(y = predict(fit_fixed0), col = "Fixed 0")) + geom_line(aes(y = 1 + coef(fit_fixed1) * x, col = "Fixed 1"))
grade_code()
Load the tidyverse
library, then read the file tutor_examples("exo_fit.txt")
into a tibble
{.R} called tofit
.
library(___) # tutor_examples("exo_fit.txt") tofit <- ___ tofit
library(tidyverse) tofit <- read_table(tutor_examples("exo_fit.txt"))
grade_code()
Plot all columns as a function of x
to see your data. For this, use on-the-fly the function pivot_longer()
{.R} to turn the tibble into a tidy one:
tofit <- read_table(tutor_examples("exo_fit.txt"))
tofit %>% pivot_longer(cols = ___, # what column do we keep? cols = -this_column values_to = ___, # name of the column gathering the values names_to = ___ # names of the column gathering the variable names ) %>% ggplot(aes(x = ___, y = ___)) + # what columns contain the x and y values? geom_point() + # plot with points facet____(~ ___, scales = ___) # put the plots on a grid varying according to a column, with free scales
tofit %>% pivot_longer(cols = -x, values_to = "y", names_to = "variable" ) %>% ggplot(aes(x = x, y = y)) + geom_point() + facet_wrap(~variable, scales="free")
grade_code()
Using lm()
{.R} or nls()
{.R} fit each column as a function of x
and display the "experimental" data and the fit on the same graph.
+ Tip: Take a look at the function dnorm()
{.R} to define a Gaussian
tofit <- read_table(tutor_examples("exo_fit.txt"))
fit_y <- lm(data = ___, ___) # what is the dataframe? what do we fit against what? # predict(fit_y) # coef(fit_y) P1 <- ___ %>% # what dataframe are we working on? ggplot(aes(x = ___, y = ___)) + # what are the x and y values of the plot? geom____() + # plot the data to fit with points geom_line(___, col = ___) # how do we add the fitted values with a red line? # # # # # # # # # # # # # # # # # # # # # # # fit_z <- nls(data = ___, # the data ___, # the function start = list(___) # the list of starting parameters ) P2 <- ___ %>% ggplot(___) + ___ # # # # # # # # # # # # # # # # # # # # # # # fit_w <- lm(___) # how can we fit w with a linear function? P3 <- ___ %>% ggplot(___) + ___ # # # # # # # # # # # # # # # # # # # # # # # fit_u <- lm(___) # how can we fit u with a linear function? P4 <- ___ %>% ggplot(___) + ___ # # # # # # # # # # # # # # # # # # # # # # # fit_s <- ___ # linear or non-linear fit? P5 <- ___ %>% ggplot(___) + ___ # # # # # # # # # # # # # # # # # # # # # # # library(patchwork) # to gather the plots on a grid easily P1 + P2 + P3 + P4 + P5 # plot on a grid
fit_y <- lm(data = tofit, y ~ x) P1 <- tofit %>% ggplot(aes(x = x, y = y)) + geom_point() + geom_line(aes(y = predict(fit_y)), col = "red") # # # # # # # # # # # # # # # # # # # # # # # fit_z <- nls(data = tofit, z ~ A * dnorm(x, mean, sd), start = list( A = 8, mean = 4, sd = 1 )) P2 <- tofit %>% ggplot(aes(x = x, y = z)) + geom_point() + geom_line(aes(y = predict(fit_z)), col = "red") # # # # # # # # # # # # # # # # # # # # # # # fit_w <- lm(data = tofit, log(w) ~ x) P3 <- tofit %>% ggplot(aes(x = x, y = log(w))) + geom_point() + geom_line(aes(y = predict(fit_w)), col = "red") # # # # # # # # # # # # # # # # # # # # # # # fit_u <- lm(data = tofit, exp(u) ~ x) P4 <- tofit %>% ggplot(aes(x = x, y = exp(u))) + geom_point() + geom_line(aes(y = predict(fit_u)), col = "red") # # # # # # # # # # # # # # # # # # # # # # # fit_s <- lm(data = tofit, log(s) ~ x) P5 <- tofit %>% ggplot(aes(x = x, y = log(s))) + geom_point() + geom_line(aes(y = predict(fit_s)), col = "red") # # # # # # # # # # # # # # # # # # # # # # # library(patchwork) P1 + P2 + P3 + P4 + P5
grade_code()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.