This repository has been archived on 2026-05-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Data-Analytics/Assignment VI/DataAnalytics2025Fall_A6_Itamar_Oren-Naftalovich.md
T
2025-12-05 19:59:00 -05:00

18 KiB
Raw Blame History

News Popularity in Multiple Social Media Platforms

This project analyzes the News Popularity in Multiple Social Media Platforms dataset from the UCI Machine Learning Repository. The data contains ~93k news items collected between November 2015 and July 2016, with their final popularity on Facebook, Google+ and LinkedIn across four topics: economy, microsoft, obama and palestine.


1. Exploratory Data Analysis

1.1 Data overview and cleaning

We work primarily with Data/News_Final.csv, which has 93,239 rows and 11 variables:

  • IDLink numeric id of the article
  • Title, Headline short text fields
  • Source news outlet that originally published the story
  • Topic one of {economy, microsoft, obama, palestine}
  • PublishDate publication timestamp
  • SentimentTitle, SentimentHeadline numeric sentiment scores derived from title and headline text
  • Facebook, GooglePlus, LinkedIn final popularity on each social media platform

According to the dataset documentation, -1 in the popularity variables indicates that no final popularity value was observed. In the code, any value < 0 in Facebook, GooglePlus, or LinkedIn is therefore replaced with NaN. Missing popularity values are later dropped on a permodel basis. PublishDate is converted to a proper timestamp, and a numeric time feature

DaysSinceEpoch = days since 1970-01-01

is created to allow inclusion of temporal trends in the models. We also logtransform Facebook popularity:

log_Facebook = log1p(Facebook)

which is used as the target for regression models.


1.2 Popularity distributions

A histogram of Facebook share counts on a logarithmic xaxis, after removing missing and zero values

distribution of facebook popularity

Figure 1: Distribution of Facebook popularity on a log xaxis.

The distribution is extremely rightskewed:

  • Most articles receive very few shares.
  • A small number of “viral” articles receive thousands of shares.

On the cleaned data, summary statistics for Facebook shares are approximately:

  • median is approx 8
  • mean is approx 129
  • 90th percentile is approx 214
  • 99th percentile is approx 2,322
  • max = 49,211

Google+ and LinkedIn exhibit similar heavytailed patterns (with smaller absolute scales), which matches the description of the dataset creators (arXiv).

The distribution of log1p(Facebook)

distribution of log-transformed facebook popularity

Figure 2: Distribution of logtransformed Facebook popularity.

The log transform compresses the heavy tail and produces a more regular, unimodal distribution. This justifies using log1p(popularity) as the regression target: it reduces the influence of rare extreme outliers while keeping them in the data, which is important because viral stories are the phenomena of interest.


1.3 Topic effects

The four topics are not equally represented:

  • economy: 33,928 items
  • obama: 28,610
  • microsoft: 21,858
  • palestine: 8,843

The mean logFacebook popularity by topic.

average facebook popularity by topic

Figure 3: Mean logFacebook popularity by topic.

Key observations:

  • obama stories clearly have the highest average popularity.
  • microsoft is slightly above economy and palestine.
  • In original share counts, obama articles average roughly an order of magnitude more shares than economy/microsoft/palestine stories, but all topics remain strongly skewed.

This suggests that topic is an important categorical predictor for popularity, and motivates including it as a onehot encoded feature in the models.


1.4 Sentiment and popularity

Sentiment scores from the title and headline are continuous values roughly in the interval [-1, 1]. Their empirical distributions are centered very close to 0 with standard deviations around 0.14, indicating that most titles and headlines are only mildly positive or negative.

A 5,000row sample of SentimentTitle vs log_Facebook

title sentiment vs facebook popularity

Figure 4: Scatter of title sentiment vs logFacebook popularity (sample of 5,000 articles).

The scatter plot shows:

  • A dense vertical band near sentiment 0, reflecting many neutral titles.
  • Viral and nonviral articles scattered across the full sentiment range, with no obvious linear trend.

Empirically, the correlation between sentiment and Facebook popularity is almost zero (|r| is approx 0.01). This suggests that sentiment alone is a weak predictor of popularity; we still include it in models because it may interact with topic or time, but we do not expect it to explain much variance by itself.


1.5 EDA conclusions

From the exploratory analysis we conclude:

  1. Popularity variables are nonnegative, highly skewed, and heavytailed.

    • Logtransforming shares yields more regular distributions, so regression models should target log1p(popularity) instead of raw counts.
  2. Topic has a strong effect on expected popularity.

    • Particularly, obamarelated news is more popular on Facebook; microsoft is relatively stronger on LinkedIn (from descriptive statistics, not shown here).
  3. Title/headline sentiment has little linear relationship with popularity.

    • It should not be expected to drive predictions strongly.
  4. There are many extreme outliers (viral stories), but these are the signal we care about.

    • We choose not to remove them; instead, we rely on robust models and logtransformed targets.

These observations motivate a modeling strategy that combines:

  • Linear models (to quantify simple topic/sentiment effects on logpopularity).
  • Nonlinear treebased models (to capture complex relationships and heavytailed behaviour).
  • Classification of viral vs nonviral stories.
  • Clustering of timeseries trajectories to identify typical growth patterns.

The next section formalizes these ideas.


2. Model Development, Validation and Optimization

We develop five models: three regression models (including a dimensionreduced variant), one classification model, and one clustering model. This covers regression, classification and unsupervised learning objectives, and explicitly examines the impact of dimensionality reduction.

All supervised models use:

  • Train/test split: 80% training, 20% test, random_state=42.

  • Evaluation on the heldout test set only (no peeking).

  • Metrics:

    • Regression: R² and RMSE on logscale (using root_mean_squared_error).
    • Classification: accuracy, F1 for the positive class, ROC AUC and confusion matrix.

2.1 Common preprocessing

For each model:

  1. Replace -1 in Facebook, GooglePlus, LinkedIn with NaN.
  2. Drop rows with missing values in the specific target variable.
  3. Use DaysSinceEpoch as a numeric representation of PublishDate.
  4. Where appropriate, use log_Facebook = log1p(Facebook) as the regression target.
  5. Encode Topic using onehot encoding with economy as the reference level (drop_first=True).

For timeseries models we also use Data/Facebook_Economy.csv, which stores Facebook popularity snapshots TS1TS144 every 20 minutes for economy articles. We join it with News_Final.csv on IDLink and restrict to:

  • Topic == "economy"
  • Time slices TS1TS50 as predictors (roughly first 1617 hours)
  • Final logFacebook popularity as the target

Negative TS values are interpreted as “no observed popularity yet” and are set to 0.


2.2 Regression Model 1 Linear regression on static features

Goal. Predict logFacebook popularity using only static metadata (no early popularity feedback).

  • Target: y = log_Facebook for all topics.

  • Features:

    • SentimentTitle, SentimentHeadline
    • DaysSinceEpoch (publication time)
    • Topic onehot dummies: Topic_microsoft, Topic_obama, Topic_palestine (economy is implicit baseline).

We fit an ordinary least squares linear regression on the training split and evaluate on the test set.

Results (test set):

  • R² is approx 0.157
  • RMSE is approx 1.86 in logspace

Actual vs predicted logFacebook values

model 1: actual vs predicted

Figure 5: Model 1 predictions vs actual logFacebook values.

The predictions are compressed into a narrow band, underpredicting viral articles and overpredicting lowpopularity ones. Key coefficients:

  • Topic_obama is approx +1.78 (large positive shift vs economy)
  • Topic_microsoft is approx +0.10
  • Topic_palestine is approx +0.02
  • SentimentTitle is approx 0.38, SentimentHeadline is approx 0.06
  • DaysSinceEpoch is approx 0.0007 (tiny downward trend over time)

Interpretation:

  • Topic has a clear effect (especially obama).
  • Sentiment effects are small and slightly negative.
  • The model explains only ~16% of the variance in logpopularity, confirming that static features alone are weak predictors.

2.3 Regression Model 2 Random forest on early time slices

Goal. Predict final logFacebook popularity for economy stories using early Facebook popularity time slices and sentiment.

  • Target: log_Facebook for economy topic, joined with Facebook_Economy timeseries.

  • Features:

    • TS1TS50 (early cumulative popularity counts, cleaned: negative → 0)
    • SentimentTitle, SentimentHeadline

We fit a RandomForestRegressor with:

  • 120 estimators,
  • min_samples_leaf=2,
  • max_depth=None (trees grow fully),
  • n_jobs=-1, random_state=42.

Results (test set):

  • R² is approx 0.746
  • RMSE is approx 0.86 (logscale)

Feature importances indicate:

  • TS50 alone contributes ~81% of total importance.
  • Combined sentiment variables contribute ~17%.
  • Earlier TS features each have very small marginal importance.

Thus, knowing an articles popularity after ~17 hours (TS50) is already highly predictive of its final 2day popularity. Early engagement is a much stronger signal than sentiment or publish time.


2.4 Regression Model 3 PCA + random forest (dimension reduction)

Model 3 examines the effect of dimension reduction on performance.

Instead of using all 50 TS features directly, we:

  1. Standardize TS1TS50 with StandardScaler.
  2. Apply PCA with n_components=10.
  3. Concatenate the 10 PCA components with the two sentiment features (SentimentTitle, SentimentHeadline).
  4. Train the same RandomForestRegressor as Model 2 on this 12dimensional feature space.

PCA results:

  • 1st component explains is approx 93.5% of variance.
  • First 10 components together explain is approx 99.9% of variance.

Results (test set):

  • R² is approx 0.745
  • RMSE is approx 0.87

Compared to Model 2:

  • R² decreases only slightly (0.746 → 0.745).
  • RMSE increases minimally (0.862 → 0.865).

So PCA reduces dimensionality from 50 TS features to 10 components with negligible loss of predictive performance. The first PCA components effectively summarize overall popularity level and growth pattern, which are the dominant signals for final popularity.


2.5 Classification Model 4 Logistic regression for viral vs nonviral

Goal. Classify whether an article is viral on Facebook, defined as being in the top 10% of final popularity.

  • Target:

    • viral_fb = 1 if Facebook ≥ 214 (90th percentile), otherwise 0.
    • Class distribution: ~10% positive, ~90% negative.
  • Features:

    • SentimentTitle, SentimentHeadline
    • DaysSinceEpoch
    • Topic dummies as before

We intentionally do not use timeslice features here to simulate making a decision at or before publication, when no engagement data is available yet.

We fit a LogisticRegression with max_iter=500 and class_weight="balanced" to counter class imbalance.

Results (test set):

  • Accuracy is approx 0.73

    • A naive classifier that always predicts “nonviral” would obtain is approx 0.90 accuracy, highlighting that raw accuracy is misleading under imbalance.
  • F1 (viral class) is approx 0.36

  • ROC AUC is approx 0.75

The ROC AUC of 0.75 indicates decent ranking ability: the model tends to assign higher probabilities to truly viral articles than to nonviral ones. However, at the default 0.5 threshold it generates many false positives; tuning the probability threshold would be necessary in practice depending on the business tradeoff between missing viral content and wasting attention on nonviral items.


2.6 Clustering Model 5 Kmeans on timeseries shapes

To understand typical growth trajectories of popularity, we cluster early timeseries patterns.

  • Features: TS1TS50, standardized with StandardScaler.
  • Sample: random subset of 5,000 economy+Facebook articles to keep computation manageable.
  • Algorithm: KMeans(n_clusters=3, n_init=10, random_state=42).

Results:

  • Silhouette score is approx 0.97, indicating wellseparated clusters (although partly due to one large cluster vs a few small ones).
  • Cluster sizes and mean final Facebook shares:
    cluster count mean shares median max
    0 4,978 ~37 3 7,045
    1 1 1,886 1,886 1,886
    2 21 ~2,478 1,291 8,010

Inspecting centroid timeseries (TS1, TS10, TS25, TS50):

  • Cluster 0: low TS1 (~0.3), slow growth, TS50 is approx 17 → “normal/low popularity” baseline; almost all articles.
  • Cluster 2: TS1 is approx 23, TS10 is approx 211, TS50 is approx 1,388 → early rapid takeoff and sustained growth; these are clearly viral trajectories.
  • Cluster 1: single extreme superviral outlier with TS1 is approx TS50 is approx 1,886.

Clustering therefore uncovers distinct popularity regimes: ordinary stories, viral stories, and rare superviral events.


3. Decisions and Practical Use

3.1 What do the models tell us?

1. Static metadata is not enough for precise prediction.

Model 1, using only topic, time and sentiment, explains only about 16% of the variance in logFacebook popularity. The EDA already indicated weak correlations between sentiment and engagement, and the model confirms that topic is the only strong static predictor. This means:

  • Before any user feedback is observed, we can form only a rough guess about popularity (e.g., “obama stories tend to do better”), but detailed predictions are unreliable.

2. Early engagement is the key signal.

Models 2 and 3 show that once ~16 hours of Facebook feedback are available:

  • Random forests can explain ~75% of the variance in final logpopularity.
  • PCA compresses the 50dimensional TS inputs to 10 components with essentially no loss in performance.

In practice, this means that monitoring early timeseries of shares is crucial. Stories that are already accumulating shares quickly by TS50 are extremely likely to end up as the most popular items after two days.

3. Logistic regression is useful for ranking, not for definitive labels.

The viral vs nonviral classifier has:

  • Good ranking ability (ROC AUC ~0.75).
  • Moderate F1 score and relatively low accuracy compared to the majority baseline.

This makes it better suited as a priority score than as a hard decision rule. For example, an editorial team might sort draft stories by predicted viral probability to decide where to invest additional editorial resources, but should not automatically discard stories predicted to be nonviral.

4. Clustering uncovers growth archetypes.

Kmeans reveals three typical growth shapes:

  1. Slow/low growth (most items).
  2. Clearly viral trajectories.
  3. A tiny number of superviral events.

Recognizing that an articles early TS pattern matches the viral or superviral cluster can trigger decisions such as:

  • Featuring the article more prominently on the homepage.
  • Allocating budget for promoted posts.
  • Producing followup content while interest is high.

3.2 How useful are these models for real decisions?

A practical decision workflow informed by this analysis could be:

  1. Prepublication / immediately at publication

    Use the logistic regression model and static features (topic, sentiment, time) to assign each new article a baseline probability of becoming viral. This can help prioritize which stories to monitor more closely, but should not be the sole basis for publication decisions.

  2. Early postpublication (first few hours)

    Once some timeslice information is available (TS1TS10), use clustering to see whether the articles early trajectory resembles known viral patterns. Articles already in the viral cluster are good candidates for early promotion.

  3. Midwindow (around TS50)

    At ~1617 hours, feed TS1TS50 into the PCA + random forest regressor (Model 3) to estimate final reach. This estimate can guide decisions about:

    • How long to keep the story on front pages.
    • Whether to schedule followups or derivative content.
    • Where to allocate marketing/promotional resources.
  4. Limitations

    • Popularity is still highly stochastic; even with R² is approx 0.75 in the best case, there is considerable residual uncertainty.
    • Models trained on this dataset focus on four specific topics and a particular time period (20152016). Performance may degrade when applied to different domains, languages or time spans. (arXiv)

Overall, these models are best used for relative ranking and triage and help in deciding which articles deserve extra attention rather than for exact point predictions of future share counts. Combining static features, early engagement signals, and growthpattern clustering yields a practical decision support tool for newsrooms and social media teams working with limited resources.

If you actually read this far...nice! :D