RStudio - Linear Regression with Groups - r

So I have 2 groups and an x and y variable. I am trying to run a linear regression to see if there is a significant relationship between the x and y variables within each group but I also want to look at the significance between groups. Then I would like to plot those results and provide a p-value, equation, and R^2 value on the graph. How would I go about accomplishing this?
I am able to plot the data on the same graph using this code:
ggplot(data_NeuroPsych, aes(x = Flanker_Ratio, y = Neuropsych_Delta, color = Group)) +
geom_point() +
geom_smooth(method = "lm", fill = NA)
Then using this open source code I was able to look at the results separately: https://github.com/kassambara/ggpubr/blob/master/R/stat_regline_equation.R#L7
The issue with the above is the data is not on the same plot and it does not look at the comparison between groups.

Related

Can the Pearson's correlation coefficient be calculated on a linear-log ggplot without being impacted by the log scale?

Hello and welcome to my question.
What I want is to plot data from two columns as a linear-log scatterplot and then calculate the Pearson's correlation coefficient using the ggpubr::stat_cor() function. The coefficient needs to be calculated on the plot, since the analysis will involve a lot of exploration; calculating separately would be laborious.
Example Dataset
library(tidyverse)
library(ggpubr)
df <- tibble(A = c(1:10), B = c(1:10))
ggplot(data = df, aes(x = A, y = B)) +
geom_point() +
stat_cor(aes(label = ..r.label..), method = "pearson") +
scale_x_continuous(trans = "log10")
The issue is that, while I can run this code without the scale_x_continuous() and get what I expect (R = 1), I need the x-axis to be in log scale in my actual code. When I add the log scale, stat_cor() calculates the coefficient with the log values, resulting in an unexpected correlation (R = 0.95).
The bottom line is that I'd like a solution that allows me to plot the x-axis on a log scale without impacting the stat_cor() function, so that the function calculates the coefficient on the original (linear) values.

Put two linear regression lines into one plot

I initially have two data frame("icelandma,swissfe") with observations from different countries and totally same variables. To compare the linear regression of intp.trust and confidence for these two countries. I combine these two data frame into one with this command:
merge1 <- rbind(icelandma, swissfe)
And then, I draw the linear regression plot with this command:
ggplot(data = merge1,aes(x=intp.trust,y=confidence))+
geom_point(size=0.5)+
geom_smooth(method = "lm",formula = y~x)+
facet_grid(countryname~.)
The plot is like this
The regression line is still in two plots, I'm wondering if there is any way to post these two lines in the same plot. Thanks for your help in advance!
Try
ggplot(data = merge1,aes(x=intp.trust,y=confidence, group = countryname))+
geom_point(size=0.5)+
geom_smooth(method = "lm",formula = y~x)
facet_wrap puts your plots in different panels by countryname~.
If you want to differentiate by countryname add color to your aes: aes(...,color = countryname).

Using interplot to plot continuous*continuous interactions for multiple individuals

I want to produce a plot which demonstrates the effects of a continuous by continuous interaction, with one line for each individual in the dataset. I have managed to successfully plot the interaction at the population level using interplot:
m2<-lmer(RMR~No_Squares*Temperature+(Temperature|ID), data=female1)
interplot(m = m2, var1 = "No_Squares", var2 = "Temperature", ci=FALSE)
But I am at a loss as how to produce such a plot for each individual ID (i.e. to show the differences between individuals)
I have tried adding:
+ geom_smooth(aes(group = ID), method = "lm")
To the code, but this doesn't work.
Any ideas?

Plotting fitted response vs observed response

I have a model which has been created like this
cube_model <- lm(y ~ x + I(x^2) + I(x^3), data = d.r.data)
I have been using ggplot methods like geom_point to plot datapoints and geom_smooth to plot the regression line. Now the question i am trying to solve is to plot fitted data vs observed .. How would i do that? I think i am just unfamiliar with R so not sure what to use here.
--
EDIT
I ended up doing this
predicted <- predict(cube_model)
ggplot() + geom_point(aes(x, y)) + geom_line(aes(x, predicted))
Is this correct approach?
What you need to do is use the predict function to generate the fitted values. You can then add them back to your data.
d.r.data$fit <- predict(cube_model)
If you want to plot the predicted values vs the actual values, you can use something like the following.
library(ggplot2)
ggplot(d.r.data) +
geom_point(aes(x = fit, y = y))

easy way to plot combined model in ggplot2

I have a data-set which has 3 columns: date, amount, and a factor/cluster. For example:
date;amount;cluster_id
02.10.10;-13,86;3
04.10.10;-66,28;3
06.10.10;-14,99;3
25.10.10;-20,96;3
30.10.10;-408,99;3
31.01.11;-29,5;2
07.02.11;-652,85;3
19.09.11;-277,48;3
30.09.11;-6,18;3
03.10.11;-242,47;3
04.11.11;-299,77;3
20.02.12;-367,85;3
03.10.12;-4,99;4
13.09.13;-6,59;4
14.10.13;-1043,46;3
24.10.13;-373,99;3
24.10.13;-1321,91;3
18.12.13;-24,45;4
03.02.14;-66,87;3
30.08.14;-7,6;2
28.10.14;-115;3
13.12.14;-8,99;3
15.12.14;-352,44;3
19.12.14;115;3
08.07.15;-59;2
The following code:
ggplot(data, aes(x=date, y=amount, colour=factor(mycluster))) +
stat_smooth(method = "rlm", formula = y ~ x)
simply performs a rlm per group/factor. And looks like:
How can I combine each separate regression model into one big (added) model in order to plot one "combined" model in an easy way e.g. without looping over all the rlm models manually.

Resources