HW exercise 1.

Fifty male and fifty female students fill out the same questionnaire in weekly intervals starting five weeks before an important examination to measure state anxiety. The research interests are:

  1. whether there are gender difference in state anxiety
  2. individual differences in state anxiety

Explore the answers to both questions with plots involving confidence intervals or error bars for the means.

Source: Von Eye, A., & Schuster C. (1998). Regression Analysis for Social Sciences. San Diego: Academic Press.

  • Column 1: Anxiety score 5 weeks before exam for female
  • Column 2: Anxiety score 4 weeks before exam for female
  • Column 3: Anxiety score 3 weeks before exam for female
  • Column 4: Anxiety score 2 weeks before exam for female
  • Column 5: Anxiety score 1 weeks before exam for female
  • Column 6: Anxiety score 5 weeks before exam for male
  • Column 7: Anxiety score 4 weeks before exam for male
  • Column 8: Anxiety score 3 weeks before exam for male
  • Column 9: Anxiety score 2 weeks before exam for male
  • Column 10: Anxiety score 1 weeks before exam for male

Load in the data set and check its structure.

'data.frame':   50 obs. of  10 variables:
 $ f1: int  13 26 13 22 18 32 16 18 14 20 ...
 $ f2: int  17 31 17 24 19 31 16 22 17 19 ...
 $ f3: int  18 33 24 26 19 30 21 25 23 23 ...
 $ f4: int  20 38 29 27 22 31 27 29 21 25 ...
 $ f5: int  24 42 32 29 30 32 30 35 25 28 ...
 $ m1: int  6 4 17 19 12 11 14 9 12 11 ...
 $ m2: int  14 11 25 22 21 16 23 18 16 13 ...
 $ m3: int  22 14 26 26 21 20 26 20 23 17 ...
 $ m4: int  20 12 29 30 23 19 29 20 26 14 ...
 $ m5: int  24 23 38 34 24 22 33 24 32 20 ...

Trun the data set into a long form and do some variable transformation.

[Note]: Column 1, `f1, is anxiety score 5 weeks before exam for female. fk is anxiety score k weeks before exam for female. mk is anxiety score k weeks before exam for male. Thus, we should use 6 to minus column index to indicate correct no. of week before the exam.

'data.frame':   500 obs. of  5 variables:
 $ values: int  13 26 13 22 18 32 16 18 14 20 ...
 $ ind   : Factor w/ 10 levels "f1","f2","f3",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ id    : int  1 2 3 4 5 6 7 8 9 10 ...
 $ gender: chr  "f" "f" "f" "f" ...
 $ weeks : num  5 5 5 5 5 5 5 5 5 5 ...

Q1: Data visualization for gender difference

Use qplot to draw boxplots to quickly get an insight.

It seems that there is gender difference in each no. of week before the exam.

Draw the plot with mean dots and error bars as the question asks.

  1. Group the data by gender and weeks and compute the mean score and the standard error for each group (gender x week).
  2. Use ggplot to plot with weeks as x and group_MEAN as y.
  3. Add data points on the plot. Assign differen colors for different genders.
  4. Add error bars on the plot. Assign the width and the size of the bars.
  5. Name the axises.
  6. Change gender label and assign the color for different gender

It seems that there is gender difference in each no. of week before the exam. The gender difference gets smaller as the no. of week before exam decreases.


Q2: Data visualization for individual difference

Plot data with regression line for each id. Use different colors for major slopes and minor slopes.

The association of weeks and anxiety score is positive in a few participants. In most of participants, the slopes of linear models of weeks as x and anxiety score as y do not differ too much.

Since we have already found that gender effect is significant, we should group gender when plotting individual difference.

  1. Group the data by gender and weeks and compute the mean score and the standard error for each group (gender x week).
  2. Use ggplot to plot with weeks as x and group_MEAN as y.
  3. Add data points on the plot. Assign differen colors for different genders.
  4. Add error bars on the plot. Assign the width and the size of the bars.
  5. Name the axises.
  6. Change gender label and assign the color for different gender

It seems that there is no obvious individual difference in neither male data. While in female data, there seems to be individual difference.

Test for gender difference and individual difference


Error: id
   Df Sum Sq Mean Sq
id  1   3429    3429

Error: id:gender
       Df Sum Sq Mean Sq
gender  1   1215    1215

Error: Within
                 Df Sum Sq Mean Sq F value   Pr(>F)    
gender            1    206     206   7.853  0.00527 ** 
weeks             1   9641    9641 366.932  < 2e-16 ***
id:weeks          1    478     478  18.182 2.41e-05 ***
gender:weeks      1    184     184   7.000  0.00841 ** 
id:gender:weeks   1     20      20   0.754  0.38579    
Residuals       492  12927      26                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

When the time effect (weeks) get controlled. The main effect of gender is still statistically significant (\(p<.01\)). There is a significant difference in anxiety score between females and males.

As for the interaction term, the interaction of gender and weeks is significant (\(p<.01\)), indicating that the gender differences differ among different no. of week before exam. The interaction of id and weeks is significant as well (\(p<.001\)), indicating that the time effect (weeks) differ among different participants.

By ANOVA results and plots about gender difference, we can know that:

  1. Females have much higher anxiety scores than males.
  2. The gender difference gets smaller as the no. of week before exam decreases. That is, as the exam time approaches, the anxiety degrees that males increase are more than girls’.

By ANOVA results and plots about individual difference, we can know that:

  1. There is a slight individual difference in anxiety score.
  2. There is individual differences in the time effect on anxiety score.


HW exercise 3.

The dataset consists of a sample of 14 primary school children between 8 and 12 years old. The children were asked to respond on 8 emotions and coping strategies scales for each of 6 situations: fail to fulfill assingments in class, not allowed to play with other children, forbidden to do something by the teacher, victim of bullying, too much school work, forbidden to do something by the mother.

Plot the data in some meaningful ways. You may have to manipulate data into a different format first.

  • Column 1: Unpleasant (Annoy)
  • Column 2: Sad
  • Column 3: Afraid
  • Column 4: Angry
  • Column 5: Approach coping
  • Column 6: Avoidant coping
  • Column 7: Social support seeking
  • Column 8: Emotional reaction, especially agression
  • Column 9: Situation ID
  • Column 10: Children ID

Source: Roeder, I., Boekaerts, M., & Kroonenberg, P. M. (2002). The stress and coping questionnaire for children (School version and Asthma version): Construction, factor structure, and psychometric properties. Psychological Reports, 91, 29-36.


[Solution and Answer]

Load in the data set and check the data structure

'data.frame':   84 obs. of  10 variables:
 $ annoy    : int  4 4 2 4 4 4 3 3 3 4 ...
 $ sad      : int  2 4 2 3 2 3 2 1 1 4 ...
 $ afraid   : int  2 4 2 4 1 1 2 1 1 2 ...
 $ angry    : int  2 2 2 4 1 4 2 2 2 1 ...
 $ approach : num  1 4 2.67 4 1 2.33 2 1.33 1 1.67 ...
 $ avoid    : num  2 3 3 1.5 2.75 2.5 1 4 1 4 ...
 $ support  : num  1 1.25 1 3.25 1.25 1 1.5 2.75 1.33 3.5 ...
 $ agressive: num  2.5 1.5 2.33 1 1.5 3.67 1 2 1.67 2.5 ...
 $ situation: Factor w/ 6 levels "Bully","Fail",..: 2 4 5 1 6 3 2 4 5 1 ...
 $ sbj      : Factor w/ 14 levels "S135","S137",..: 6 6 6 6 6 6 4 4 4 4 ...
     annoy            sad           afraid          angry      
 Min.   :1.000   Min.   :1.00   Min.   :1.000   Min.   :1.000  
 1st Qu.:2.000   1st Qu.:1.00   1st Qu.:1.000   1st Qu.:1.000  
 Median :3.000   Median :2.00   Median :1.000   Median :2.000  
 Mean   :2.762   Mean   :1.81   Mean   :1.405   Mean   :2.131  
 3rd Qu.:4.000   3rd Qu.:2.00   3rd Qu.:2.000   3rd Qu.:2.250  
 Max.   :4.000   Max.   :4.00   Max.   :4.000   Max.   :9.000  
                                                               
    approach         avoid          support        agressive    
 Min.   :1.000   Min.   :1.000   Min.   :0.000   Min.   :1.000  
 1st Qu.:1.670   1st Qu.:1.750   1st Qu.:1.330   1st Qu.:1.000  
 Median :2.000   Median :2.500   Median :2.000   Median :1.500  
 Mean   :2.236   Mean   :2.398   Mean   :1.959   Mean   :1.542  
 3rd Qu.:3.000   3rd Qu.:3.000   3rd Qu.:2.373   3rd Qu.:1.670  
 Max.   :4.000   Max.   :4.000   Max.   :4.000   Max.   :4.000  
                                                                
  situation       sbj    
 Bully :14   S135   : 6  
 Fail  :14   S137   : 6  
 MomNo :14   S139   : 6  
 NoPart:14   S17    : 6  
 TeacNo:14   S185   : 6  
 Work  :14   S2     : 6  
             (Other):48  

There is something weird in angry, which has the maximum of 9 while all maximums of other scale scores are 4. Let’s check it.


 1  2  3  4  9 
25 38 11  9  1 

Only one data point is 9 in angry. Actually, only this data point have angry score exceeding 4. Thus, I believe that this is a wrong data point. Let’s remove it.

Lollipop plot 1 - y: scale scores, facet: situations

Data transformation

  1. Standardize the scores for each emotion scale.
  2. Stack the data set.
  3. Group the data set by situation and variable (scales). Compute the mean of standardized scores for 6 situations and 8 scales.
  • See the basic info of the transformed data set.
  situation     variable      value           
 Bully :8   annoy   : 6   Min.   :-0.8214336  
 Fail  :8   sad     : 6   1st Qu.:-0.2096925  
 MomNo :8   afraid  : 6   Median :-0.0235430  
 NoPart:8   angry   : 6   Mean   :-0.0002002  
 TeacNo:8   approach: 6   3rd Qu.: 0.2692385  
 Work  :8   avoid   : 6   Max.   : 0.6997865  
            (Other) :12                       
  • Randomly pick one row for each situation to check the transformed data set.

Looks great.

Plot

This plot can help us to see what kind of situation tend to trigger the given emotion more or less than other situations. Take two examples:

  1. Children have much higher scores of “sad” in situation of being a victim of bullying than any other situations.
  2. Children have much lower scores of “avoid” in the situation of failing to fulfill assingments in class than any other situations.

Box plot 1

Box plot 1-1: Boxt plot of standardized emotion scores

This plot can tell us there is a larger/smaller variation among 8 emotions in what kind of situation.

Box plot 1-2: Box plot of standardized emotion scores with faceting situations

This plot can tell us what kind of situation has a larger/smaller variation in one given emotion. For example, compared to other situation, situation of being a bully victim has larger variation in agressive emotion. That is, when being a bully victim, the difference of tne degree of agressive emotion among chidren is relation bigger than that of other emotions.

Finding

From these 2 plots, we can find that, for example, the situation “bully” has relative smaller variation among 8 emotions than other situations while it has relative larger variation among 14 children in angry emotion (the median even equals 0). That is, when being a bully victim, the difference among 8 emoitons is not very big while childern differ in expressing angry emotion.


Lollipop plot 2 - y: situations, facet: scale scores

In this plot, we exchange y and facet variable to see different comparsions.

Data transformation

  1. Stack the data set.
  2. Standardize the scores for each situation.
  3. Group the data set by variable (scales) and situation. Compute the mean of standardized scores for 6 situations and 8 scales.
  • See the basic info of the transformed data set.
     variable   situation     value           
 annoy   : 6   Bully :8   Min.   :-1.1294166  
 sad     : 6   Fail  :8   1st Qu.:-0.3436152  
 afraid  : 6   MomNo :8   Median :-0.1051407  
 angry   : 6   NoPart:8   Mean   : 0.0002451  
 approach: 6   TeacNo:8   3rd Qu.: 0.3728867  
 avoid   : 6   Work  :8   Max.   : 0.9547457  
 (Other) :12                                  
  • Randomly pick one row for each emotion to check the transformed data set.

Looks great.

Plot

This plot can help us to see what kind of emotion tend to be triggered more or less than other emotion in the given situation. Take two examples:

  1. Children have much higher scores of “annoy” than other emotions in the situation of being a victim of bullying.
  2. Children have much lower scores of “afraid” than other emotions in the situation of being forbidden to do something by the mother.

Box plot 2

Box plot 2-1: Boxt plot of standardized emotion scores

This plot can tell us what kind of emotion has a larger/smaller variation among six stiuations.

Box plot 2-2: Box plot of standardized emotion scores with faceting situations

This plot can tell us what kind of emotion has a larger/smaller variation in one given stiuation.

Finding

From these plots, we can find that, for example, the emotion “annoy” has relative smaller variation among 6 situations than other emotions while it has relative larger variation among 14 children when being the same situation.

Correlation plot for scales

When being in the same situation, there is a medium positive correlation (\(.2 < r <. 8\)) between these emotions:

  1. annoy and sad
  2. annoy and angry
  3. avoid and sad
  4. afraid and sad
  5. afraid and approach
  6. angry and aggressive


HW exercise 4 (Need help).

Revise the plot in the contrast detection example so that the plot is faceted by subject × spatial frequency condition.

The data sets are from a psychophysical experiment where five participants detected sine wave gratings at different contrasts and spatial frequencies. At each trial, a binary response (grating left or right) which is either correct or incorrect, is to be scored. Each row in the data frame represents observation from a single trial. The basic plot puts contrast on the x-axis and the proportion of correct responses on the y-axis. The contrast values in the experiment were sampled logarithmically. The aim of the experiment was to see whether and how human visual sensitivity to contrast changes depending on the spatial scale of the information (loosely, whether the pattern is coarse or fine). The researchers also want to compare how the performance shifts as a function of contrast within each subject. But the plot below shows the averaged responses from all subjects, ignoring spatial frequencies.

  • Column 1: Subject ID
  • Column 2: Contrast
  • Column 3: Spatial frequency
  • Column 4: Target side
  • Column 5: Response side
  • Column 6: Unique trial ID (not used)

Target output 2

Target output 2


Codes for original plot

Load in the data sets together and check its structure

'data.frame':   7000 obs. of  6 variables:
 $ subject    : chr  "S1" "S1" "S1" "S1" ...
 $ contrast   : num  0.0695 0.0131 0.0695 0.0695 0.3679 ...
 $ sf         : num  0.5 40 4.47 40 13.37 ...
 $ target_side: Factor w/ 2 levels "left","right": 2 2 1 1 1 1 2 2 1 2 ...
 $ response   : Factor w/ 2 levels "left","right": 2 1 1 2 1 2 2 1 2 2 ...
 $ unique_id  : chr  "544ee9ff-2569-4f38-b04e-7e4d0a0be4d2" "b27fe910-e3ba-48fb-b168-5afb1f115d8f" "72c9d6ce-0a90-4d4b-a199-03435c15291b" "48b5bbb2-e6ee-4848-b77e-839ed5320c01" ...

Data transformation and visualization

  • Score the correct responses.
   subject             contrast              sf         target_side 
 Length:7000        Min.   :0.002479   Min.   : 0.500   left :3500  
 Class :character   1st Qu.:0.005704   1st Qu.: 1.495   right:3500  
 Mode  :character   Median :0.030197   Median : 4.472               
                    Mean   :0.092678   Mean   :11.968               
                    3rd Qu.:0.159880   3rd Qu.:13.375               
                    Max.   :0.367879   Max.   :40.000               
  response     unique_id            correct      
 left :3488   Length:7000        Min.   :0.0000  
 right:3512   Class :character   1st Qu.:1.0000  
              Mode  :character   Median :1.0000  
                                 Mean   :0.7697  
                                 3rd Qu.:1.0000  
                                 Max.   :1.0000  
  • Bootstrapped CIs for proportion of correct responses.
  • Links for Binomial Family for m-alternative Forced-choice. Get document info.
  • Fit a detection model in which the chance of correct response is \(.5\).
  • Generate predicted responses.
  • Augument to the observed plot.

The example codes seem to have something wrong: they tried to bootstrapped CIs for correct, a binary variable, instead of the proportion of correct responses. Thus, I tried figure out the solution to fix it in the following faceted plots.

Revise the plot to make it faceted by subject × spatial frequency condition.

Check the distribution of sf to group


  S1   S2   S3   S4   S5 
1400 1400 1400 1400 1400 
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.500   1.495   4.472  11.968  13.375  40.000 

Data transformation

  1. Group sf by the quantiles. Name the variable with group labels sf_group.
  2. Split the data set twice: Split by subject first and then split by sf_group.
  3. Do such tasks for each data frame in the double list:
  1. Fit a detection model in which the chance of correct response is \(.5\).
  2. Generate predicted responses.
  3. Return a data frame with predicted values.
  1. Bind rows of each data frame in the double list to turn the data set into a data frame.
   subject            sf_group            contrast           y        
 Length:20000       Length:20000       Min.   :0.002   Min.   :0.500  
 Class :character   Class :character   1st Qu.:0.006   1st Qu.:0.502  
 Mode  :character   Mode  :character   Median :0.030   Median :0.858  
                                       Mean   :0.093   Mean   :0.769  
                                       3rd Qu.:0.160   3rd Qu.:1.000  
                                       Max.   :0.368   Max.   :1.000  
                                       NA's   :13000   NA's   :13000  
      idx              xval               yval       
 Min.   :   1.0   Min.   :0.002479   Min.   :0.5000  
 1st Qu.: 250.8   1st Qu.:0.093829   1st Qu.:0.9911  
 Median : 500.5   Median :0.185179   Median :0.9999  
 Mean   : 500.5   Mean   :0.185179   Mean   :0.9473  
 3rd Qu.: 750.2   3rd Qu.:0.276529   3rd Qu.:1.0000  
 Max.   :1000.0   Max.   :0.367879   Max.   :1.0000  
                                                     

Plot

  1. Dot: The real data in the data set (contrast) and its predicted values (y) based on the model (m0), that is, the proportion of correct responses.
  2. Curve: The synthetic data (xval) and its predicted values (yval) based on the model (m0).

No error bar appears beacause every predicted value in the given contrast is the same.

Try to make error bars (make variation in predicted values): Generate several times of synthetic data

Change: Generate synthetic data with fewer elements but for several times.

   subject            sf_group            contrast              y         
 Length:7000        Length:7000        Min.   :0.002479   Min.   :0.5000  
 Class :character   Class :character   1st Qu.:0.005704   1st Qu.:0.5018  
 Mode  :character   Mode  :character   Median :0.030197   Median :0.8577  
                                       Mean   :0.092678   Mean   :0.7693  
                                       3rd Qu.:0.159880   3rd Qu.:0.9998  
                                       Max.   :0.367879   Max.   :1.0000  
                                                                          
      idx             xval             yval       
 Min.   :  1.0   Min.   :0.0025   Min.   :0.5000  
 1st Qu.: 88.0   1st Qu.:0.0634   1st Qu.:0.9838  
 Median :175.5   Median :0.1852   Median :0.9999  
 Mean   :196.5   Mean   :0.1852   Mean   :0.9094  
 3rd Qu.:263.0   3rd Qu.:0.3070   3rd Qu.:1.0000  
 Max.   :560.0   Max.   :0.3679   Max.   :1.0000  
                 NA's   :2800     NA's   :2800    

Plot again

  1. Dot and bar: The synthetic data (xval) and its predicted values (yval) based on the model (m0) with boosted error bar among data coming from 30 generations.
  2. Curve: The real data in the data set (contrast) and its predicted values (y) based on the model (m0), that is, the proportion of correct responses.

However, there is still no error bar because generating synthetic data with fewer elements but for several times still makes data point unique from each other. We need to figure out the approach that can make the variation exists in the given contrast.


HW exercise 5 (Need help).

Use the Cushings{MASS} data set to generate a plot similar to the following one:

Target output 2

Target output 2

Load in the data set and check the data structure

'data.frame':   27 obs. of  3 variables:
 $ Tetrahydrocortisone: num  3.1 3 1.9 3.8 4.1 1.9 8.3 3.8 3.9 7.8 ...
 $ Pregnanetriol      : num  11.7 1.3 0.1 0.04 1.1 0.4 1 0.2 0.6 1.2 ...
 $ Type               : Factor w/ 4 levels "a","b","c","u": 1 1 1 1 1 1 2 2 2 2 ...

[Note] By using ?Cushings, we can know the meaning of each level in Cushings$Type:

  • a: Adenoma
  • b: Bilateral Hyperplasia
  • c: Carcinoma
  • u: Unknown