How to calculate slope with conditions in r

I am trying to calculate the slope of the below-listed data set ( complete excel file here new_by_particip.csv)

 stim_ending_t visbility soundvolume           Opening_text              Source.Name        m        sd   coefVar
1             1         0           0 Now focus on the Image     001_visual_demo1.csv 2.838282 1.6369315 0.5767333
2             1         0           0 Now focus on the Image     002_visual_demo1.csv 1.502490 0.5223646 0.3476660
3             1         0           0 Now focus on the Image     003_visual_demo1.csv 1.411462 0.6549790 0.4640431
4             1         0           0 Now focus on the Image 003demo_visual_demo1.csv 1.000722 0.2755927 0.2753940
5             1         0           0 Now focus on the Image     004_visual_demo1.csv 1.032277 0.3211426 0.3111013
6             1         0           0 Now focus on the Image     005_visual_demo1.csv 1.300203 0.9841223 0.7568987

Here y = m from the data set, while x = stim_ending_t ( which is a factor of 6 values "1, 1.5, 2, 2.5, 3, 3.5")

Three are three repeated conditions that I want to get the slope for each participant = Source.Name:

visbility =1 soundvolume=0 (visbility)
visbility =0 soundvolume=1 (soundvolume)
visbility = 0 soundvolume=0 (empty)

What I can do, but it will be very lengthy and tedious work, is to divide the date by each condition in Excel for each participant using =slope function!

Q1: how to calculate the slope for m against stim_ending_t while satisfying the above-mentioned conditions?

Is this what you mean?

library(tidyverse)
library(broom)

# Sample data from file
url <- "https://raw.githubusercontent.com/MohJumper/VisualAuditoryModality/master/new_by_particip.csv"
download.file(url, "data.csv")
df <- read.csv("data.csv")

df %>% 
    group_nest(Source.Name, visbility, soundvolume) %>% 
    mutate(model = map(data, ~lm(m ~ stim_ending_t, data = .x))) %>% 
    mutate(slope = map_dbl(model, ~tidy(.x)$estimate[2]))
#> # A tibble: 63 x 6
#>    Source.Name            visbility soundvolume data            model slope
#>    <fct>                      <int>       <int> <list>          <lis> <dbl>
#>  1 001_visual_demo1.csv           0           0 <tibble [12 × … <lm>  0.485
#>  2 001_visual_demo1.csv           0           1 <tibble [12 × … <lm>  0.518
#>  3 001_visual_demo1.csv           1           0 <tibble [12 × … <lm>  0.612
#>  4 002_visual_demo1.csv           0           0 <tibble [12 × … <lm>  0.691
#>  5 002_visual_demo1.csv           0           1 <tibble [12 × … <lm>  0.704
#>  6 002_visual_demo1.csv           1           0 <tibble [12 × … <lm>  0.783
#>  7 003_visual_demo1.csv           0           0 <tibble [12 × … <lm>  0.539
#>  8 003_visual_demo1.csv           0           1 <tibble [12 × … <lm>  0.428
#>  9 003_visual_demo1.csv           1           0 <tibble [12 × … <lm>  0.458
#> 10 003demo_visual_demo1.…         0           0 <tibble [12 × … <lm>  0.514
#> # … with 53 more rows

Created on 2019-07-25 by the reprex package (v0.3.0.9000)

Each topic here must be about one defined issue, I recommend you to solve your first issue and then ask this question on a new topic providing a REPRoducible EXample (reprex) and making clear what kind of graph you want to do and which are the variables you want to plot.

1 Like

Hi Andresrcs, Thank you so much for your help that is what I meant! But I used your code on my data set, I got this error Error in model.frame.default(formula = m ~ stim_ending_t, data = .x, drop.unused.levels = TRUE) : variable lengths differ (found for 'stim_ending_t') In addition: Warning message: ... is ignored in group_nest(<grouped_df>), please use group_by(..., add = TRUE) %>% group_nest()
However, when I copied your steps I got the result that I was looking for, any idea why this happened?

Sorry, I have no idea, if you could make a REPRoducible EXample (reprex) about this, we could take a look and try to help. To get sample data that better represents the structure of your actual dataframe try using dput() function (as explained in the reprex link).

1 Like

No need to worry about this because when I re-import the data set again it worked.

I also forget to add one more condition which is splitting the three conditions by (Opening_text = Image and Sound).

slope_n <- df %>% 
  +     group_nest(Source.Name, visbility, soundvolume, Opening_text) %>% 
  +     mutate(model = map(data, ~lm(m ~ stim_ending_t, data = .x))) %>% 
  +     mutate(slope = map_dbl(model, ~tidy(.x)$estimate[2]))

And here is what I got

Source.Name	visbility	soundvolume	Opening_text	slope
001_visual_demo1.csv	0	0	Now focus on the Image	0.533132088
001_visual_demo1.csv	0	0	Now focus on the Sound	0.437678643
001_visual_demo1.csv	0	1	Now focus on the Image	0.443719612
001_visual_demo1.csv	0	1	Now focus on the Sound	0.59256568
001_visual_demo1.csv	1	0	Now focus on the Image	0.651976049
001_visual_demo1.csv	1	0	Now focus on the Sound	0.572769348
002_visual_demo1.csv	0	0	Now focus on the Image	0.740229364
002_visual_demo1.csv	0	0	Now focus on the Sound	0.642380473
002_visual_demo1.csv	0	1	Now focus on the Image	0.66209081
002_visual_demo1.csv	0	1	Now focus on the Sound	0.745573256
002_visual_demo1.csv	1	0	Now focus on the Image	0.826084381
002_visual_demo1.csv	1	0	Now focus on the Sound	0.740139004

When I tried to export the data to .csv file using this code

write.table(slope_n, file = "slope_n.csv", row.names=F, sep = ",")

I got this error message unimplemented type 'list' in 'EncodeElement' which basically means some of the columns are lists instead of being factors.

So simple way to fix it using this code:

slope_n <- apply(slope_n,2,as.character)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.